2013-10-24 38 views
0

在這段代碼中是我想要實現的。我注重的部分是添加html標籤到變量jQuery的結果

'<span class="chosen">' + title + changed + '</span>'; 

當然這是爛筆頭,因爲<span></span>將不被顯示爲html標籤,但文字。

代碼:

$(window).load(function() { 
    $('.variations-table .select-option a').click(function() { 
     var title = $('.variations-table td label').text() + (' Chosen: '); 
     var change = $(this).attr("title"); 
     var phrase = '<span class="chosen">' + title + change + '</span>'; 
     $('.main-image h5').text(phrase); 
    }) 
}); 

我應該如何更改代碼有<span>標籤作爲HTML標記傳遞?

謝謝。

回答

1

以及使用html() ..instead的text()

var phrase =$('<span />',{class:"chosen",text:title + change}); 
$('.main-image h5').html(phrase); 
2

變線

$('.main-image h5').text(phrase); 

$('.main-image h5').html(phrase); 
1
$('.main-image h5').html(phrase); 

.html()採取一切裏面的HTML。即使簡單的文字。

.text()設置元素的文本屬性。

相似.val()用於設置元素的「value」屬性。