我有以下由SiteCore内容管理生成的html:
<input aria-label="Username is required." class="form" id="Username" name="Username" type="text" value="" aria-required="true">
我想做的是用以下内容替换上述内容:
<input aria-label="Email is required." class="form" id="Email" name="Email" type="text" value="" aria-required="true">
如何使用jquery或纯javascript执行此操作?
理想情况下,我想在文档加载后立即替换html。
答案 0 :(得分:1)
查看JQuery .replaceWith()
函数,该函数专门用于此目的:https://api.jquery.com/replaceWith/
您可以执行以下操作:
$('#Username').replaceWith( [ new html here ]);
使用$.ready()
功能检测文档何时完全加载:https://api.jquery.com/jQuery.ready/
$.when( $.ready ).then(function() {
// Document is ready.
$('#Username').replaceWith(`
<input aria-label="Email is required." class="form" id="Email" name="Email" type="text" value="" aria-required="true">
`);
});
请记住,在加载文档后编辑自动生成的HTML并不是一个好主意,因为它会使UI与后端预期存在的内容不同步。您可能想知道是否有办法更改生成器正在吐出的代码,因为这种方法相当脆弱。