我正在尝试将 HTML 代码获取到 jquery 变量,它在以下代码中工作:
var html_var = $(this).parent().html();
但是当我想修改“html_var”时我做不到,我的意思是,html 代码包含按钮标签,我需要更改此按钮的值和 ID?我尝试使用“查找”,但我不明白为什么它不起作用。
var modified_var = html_var.find('.button').attr('id');
最佳答案
.html() 以纯文本形式返回 html,因此您可以对父对象进行操作:
var $parent = $(this).parent();
$parent.find.('.button').attr('id', newValue);
或者你可以对文本进行操作
var html_var = $(this).parent().html();
//modify the text
$(this).parent().html(html_var); //replace the parent html with the modified html
关于javascript - JQuery 将 HTML 内容分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340473/