这是我的jQuery,我正在尝试在abc div类内添加新数据
var html = '<div class="wrapper"><div class="abc"></div></div>';
if (someData == "YES"){
var newHTML = '<div>hello</div>';
$(html).find(".abc").html(newHTML);
}
$(body).html(html);
如何获取更新的HTML?
问题是因为您从中的字符串创建了jQuery对象html
,并且修改了该对象,但是没有在任何地方存储对该对象的引用。
调用时,$(body).html()
您将添加原始字符串,而不是您更改的jQuery对象。因此,您需要将逻辑更改为此:
var html = '<div class="wrapper"><div class="abc"></div></div>';
var $html = $(html); // create the jQuery object and store a reference to it
// alternatively the above can be made in to one line:
// var $html = $('<div class="wrapper"><div class="abc"></div></div>');
if (someData == "YES") {
var newHTML = '<div>hello</div>';
$html.find(".abc").html(newHTML); // amend the jQuery object
}
$(body).html($html); // add the updated HTML to the DOM
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句