[英]html stored in javascript variable,how to Update that html inside javascript variable
这里,
var data = '
<table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 100%;">
<tbody>
<tr>
<td>
<textarea cols="80" id="editor" name="editor" rows="10">This is my textarea to be replaced with Editor.</textarea>
</td>
</tr>
<tr>
<td colspan="2">© 2016 Vishmay Shah</td>
</tr>
</tbody>
</table>
<p> </p>'
我想在数据变量中使用 jquery 将编辑器的 id 更新为 editor1
我可以使用它访问它
$('#editor', Data)[0].id=$('#editor', Data)[0].id+'1';
但我想将更新的 HTML 保存在 data 变量中。
更新
将有多个具有相同 id 编辑器的编辑器。
实际上,我想通过附加索引使其唯一。
即editor0,editor1,...,replace函数会替换所有的editor,所以无济于事
var editors = $('#editor', Data);
for (var i = 0; i < editors.length; i++) {
var subeditor = editors[i].id + i;
editors[i].id = subeditor;
}
如果您有多个具有相同 ID 名称的,也许您需要通过属性找到它,并用索引替换旧 ID 名称,如下所示:
var Data = '<table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 100%;"> <tbody> <tr><td><textarea cols="80" id="editor" name="editor" rows="10">This is my textarea to be replaced with Editor.</textarea><textarea cols="80" id="editor" name="editor" rows="10">This is my textarea to be replaced with Editor.</textarea></td> </tr> <tr> <td colspan="2">© 2016 Vishmay Shah</td> </tr> </tbody></table><p> </p>';
// this will look the element with ID editor
// and replace element with index
var newElem = $(Data).find('[id="editor"]').attr('id', function(i, old) {
return old + i;
}).end();
console.log(newElem)
DEMO (检查元素以查看实际 ID)
这将解决:
Data.replace('name="editor"','name="editor1"')
可以使用:last
, .length
at .prop(function(name, propertyValue){})
或.attr(function(name, propertyValue){})
来更新多个元素的id
属性。
另一种方法是尝试使用相同的className
进行替换,例如; .editor
,用于多个id
以名称开头并在多个元素处以数字结尾。 使用.length
确定当前正在处理的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.