2016-03-21 16 views
0

Sonuç olmayan bir şey anlamaya çalışıyorum. Basit bir <img> düğme olarak kullanıyorum ve üzerine tıklarsam, diğer <h3> öğelerinden gelen metinler değişmelidir. Bu bölüm çalışıyor, ama yeni metne bir Bullet öğesi olarak bir görüntü eklemeyi de deniyorum, ama işe yaramıyor, metin olarak da işleniyor. İşte benim gerçek kod:Metni işlevle değiştirin ve madde işaretlerini dahil edin

HTML:

<img id="button1" src="images/info.png"/> 
<h3 class="text1"> Here's some temporal text </h3> 

JAVASCRIPT:

$("#button1").click(function() { 
$(".text1").text('This will be the new text also including a bullet <img src="images/bullet.png" style="margin-right:10px;"/> And here continues the text, but seems to be that the bullet is not being rendered:(only as text'); 
}); 

Eğer bir çözüm bulmak için bana yardım eder misin?

cevap

3

kullanın .html() yerine sadece elemanın textContent ayarlayacaktır .text().text() olarak dolayı, sağlanan insert olmaz argümanı DOM Element ek bir kaynak olarak

$("#button1").click(function() { 
 
    $(".text1").html('This will be the new text also including a bullet <img src="images/bullet.png" style="margin-right:10px;"/> And here continues the text, but seems to be that the bullet is not being rendered:(only as text'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<img id="button1" src="images/info.png" /> 
 
<h3 class="text1"> Here's some temporal text </h3>

+0

olarak: arasındaki fark nedir [bkz jQuery: text() ve html()?] (http://stackoverflow.com/questions/1910794/what-is-the-difference-between-jquery-text-and-html) ve [jQuery'nin html'si için belgeler() ] (http://api.jquery.com/html/). – showdev

+0

teşekkürler @RayonDabre işe yarıyor! Sen en iyisisin, bu benim problemimin cevabı, .txt yerine .html kullan. – Eugenio