将标题属性添加到现有 HTML 的某个位置

add title attr to somewhere to existing html

本文关键字:HTML 位置 标题 属性 添加      更新时间:2023-09-26

我希望我可以将标题attr插入到现有的html中。 不确定是否可能。

http://jsfiddle.net/9X53k/

var existedHtml = "<li><a href='#'>some item</a></li>"
$('div').append(existedHtml);
// if user did some option run the below code..
// it doesnt work that way btw..
$(existedHtml).find('a').attr('title','work?');

尝试用jQuery包装它

$(somePredifinedHTML).attr('title','myTitle');

如果要提供标题,可以使用"查找"

$(somePredifinedHTML).find('a').attr('title','myTitle');

更新

像这样更改 html 后,您必须保存它

var existedHtml = "<li><a href='#'>some item</a></li>"
existedHtml = $(existedHtml).find('a').attr('title','work?');    
$('div').append(existedHtml);

演示

所以现在我知道你想要什么,这就是解决方案

var existedHtml = "<li><a href='#'>some item</a></li>"
var tester = $('div').append(existedHtml);
// if user did some option run the below code..
// it doesnt work that way btw..
tester.find('a').attr('title','work?');

您之前所做的不起作用,因为该元素已经添加到 dom 中

http://jsfiddle.net/9X53k/2/