我添加了代码以在文本后添加星号(其中包含 css 类)。当代码运行时,它显示代码而不是正在处理的 html。 代码:
jQuery(document).ready(function(){
document.getElementById("billing_state_field").getElementsByTagName('label')[0].after("<abbr class='required' title='required'>*</abbr>"));
});
输出:
<label for="billing_state" class="">STATE / COUNTY / PROVINCE / EMIRATE</label>
<abbr class='required' title='required'>*</abbr>
最佳答案
如果您正在使用 jQuery,为什么不充分利用它呢?
而不是使用
condocument.getElementById("billing_state_field").getElementsByTagName('label')[0]
使用$("#billing_state_field label",condocument).eq(0)
按照评论中的要求,我将 .after( ... )
更改为 .html( .html() + ... )
$("#billing_state_field label").eq(0).html($("#billing_state_field label").eq(0).html() + "<abbr class='required' title='required'>*</abbr>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="billing_state_field">
<label>Test </label>
</div>
关于javascript - after() 显示解析后的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49126619/