jQuery 替换跨度中的 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4093678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery replace html in span
提问by griegs
Is there an issue with replacing the contents of a <span
in jQuery.
替换<span
jQuery中 a 的内容是否存在问题。
I have this;
我有这个;
<span class='DimensionList'>
some html
</span>
and
和
$('.DimensionList').html("<b>This is the new html</b>");
What I'm finding is that in FF the contents of the span are being added to. So the new HTML sits above the old.
我发现在 FF 中,跨度的内容被添加到。所以新的 HTML 位于旧的之上。
In IE6, yes I have to cater for it, it's doing much the same.
在 IE6 中,是的,我必须迎合它,它的作用大致相同。
EDIT
编辑
I have edited the question to show that there is html in the replacement html
我已经编辑了问题以显示替换 html 中有 html
回答by hybernaut
Since there's no markup in your replacement text, use text() instead of html():
由于替换文本中没有标记,请使用 text() 而不是 html():
$('.DimensionList').text("This is the new html");
回答by griegs
if you needed to use .html() you could empty it first:
$('.DimensionList').html("").html("<b>This is the new html</b>");
如果你需要使用 .html() 你可以先清空它:
$('.DimensionList').html("").html("<b>This is the new html</b>");
回答by Jared
You've capitalized DimensionList in your javascript and it is lowercase in your HTML source. They need to be identical. In other words, it is case sensitive.
您已在 javascript 中将 DimensionList 大写,而在 HTML 源代码中则为小写。它们必须相同。换句话说,它区分大小写。
回答by JustcallmeDrago
Have you tried .text() ? It is similar to .html()
你试过 .text() 吗?它类似于 .html()
From jQuery's Documentation:
来自 jQuery 的文档:
Unlike the .html() method, .text() can be used in both XML and HTML documents
We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), which replaces special characters with their HTML entity equivalents (such as < for <).
与 .html() 方法不同,.text() 可以在 XML 和 HTML 文档中使用
我们需要注意,此方法会根据需要对提供的字符串进行转义,以便在 HTML 中正确呈现。为此,它调用 DOM 方法 .createTextNode(),该方法将特殊字符替换为其对应的 HTML 实体(例如 < for <)。