html存储在javascript变量中,如何在javascript变量中更新该html
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 保存回数据变量中。
更新
将有多个具有相同 id 编辑器的编辑器。
实际上,我想通过附加索引来使其唯一。
那就是编辑器0,编辑器1,...,替换函数将替换所有编辑器,因此无济于事
var editors = $('#editor', Data);
for (var i = 0; i < editors.length; i++) {
var subeditor = editors[i].id + i;
editors[i].id = subeditor;
}
如果您有多个具有相同 ID 名称的 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"')
更新属性id
多个元素可以使用:last
、 .length
.prop(function(name, propertyValue){})
或 .attr(function(name, propertyValue){})
来实现。
另一种方法是尝试使用相同的className
替换,例如; .editor
,表示多个id
以名称开头,在多个元素处以数字结尾。使用.length
确定当前正在处理的元素。
相关文章:
- 通过javascript重定向html传递php变量
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 来自文本输入null的html javascript变量
- 如何在HTML中显示服务器对象变量
- 在索引.html和应用.js [node.js] 之间共享变量
- 节点.js将变量显示为 HTML
- 从Javascript和Php变量创建Html模板文档
- 在if语句下的html中使用javascript变量
- 操作javascript变量[HTML]以只显示文本
- 在HTML包围的javascript中添加两个变量
- 变量不递增如何I'我喜欢html按钮点击的时候
- 将变量从html传递到javascript再传递到php
- HTML 按钮点击函数无法使用 jQuery 变量作为参数
- 使用JavaScript变量更改HTML组类
- 如何在html中将字符串转换为数字?以及如何将变量传递到scriptlet代码中
- $Scope变量在HTML中不更新,尽管在控制器中不断更新
- 使用php在html中搜索硬编码的JavaScript变量
- 将html变量从Ajax传递到PHP
- Jquery 不会正确更新带有变量 (html) 的文本框
- 如何在JQuery中克隆变量html元素