簡體   English   中英

如何在.html Jquery方法內的變量中添加跨度標簽?

[英]How to add a span tag to a variable inside a .html Jquery method?

我很難找出這段代碼為什么不起作用。 具體來說,為什么我不能跨越變量,然后再給它添加一個類? 當然,如果僅是文本而不是變量,則可以成功添加類:

var fruit = 'bananas';
var paragraph = $('<div></div>');

paragraph.html('I like ' + '<span class="features">'fruit'</span>');
$(".features").css("color","yellow");

我可以看到一些錯誤與您的代碼有關。

// fruit var is fine
var fruit = 'bananas';

// originally you had html markup in here, but you have to use html selectors.
var paragraph = $('div');

// you were close here but you didnt have plus symbols either side of your fruit var
paragraph.html('I like '+'<span class="features">'+fruit+'</span>');

// add the colour to your .features span - this is fine
$(".features").css("color","yellow");

在這里查看工作小提琴。 https://jsfiddle.net/joshmoto/k0vrs8ne/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM