jquery - IE 11 将伪内容视为元素内容的一部分

标签 jquery internet-explorer-11 pseudo-element

我遇到了一个问题,除了使用数据属性创建解决方法之外,我没有可靠的解决方案。然而,我发现我遇到的行为非常奇怪,想知道是否有更好的解决方法。

考虑以下代码:

HTML

<div id="test">This is a test!</div>

CSS

#test:after
{
    content: "...This shouldn't be part of text()!";
}

JS

$(document).ready(function () {
    $(document).on("click", "#test", function () {
        alert($(this).text());
    });
});

我正在处理的问题是,IE 11 似乎将 :after 伪选择器中的内容作为 div 中内容的一部分。这意味着当单击 div 时,IE 11 将显示一条警告:

这是一个测试!...这不应该是 text() 的一部分!

但是,我期望收到一条警报:

这是一个测试!

Firefox 和 Chrome 都显示后一个警报。我想知道是否可以让 IE 也显示后一个警报,而不使用额外的 data--attributes 等。

这是一个 FIDDLE 对于这个例子。

最佳答案

尝试将 .text() 更改为 .html(),例如:Here 。它在 IE 11.0.9600.17351 上运行良好(使用您的版本和此版本)

代码:

$(document).ready(function () {
    $(document).on("click", "#test", function () {
        alert($(this).html());
    });
});

关于jquery - IE 11 将伪内容视为元素内容的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26384885/

相关文章:

javascript - 使用 jQuery 重新排序 Div 中的内容时遇到问题

CSS:IE11 不应用样式

json - 当 Angulars $http.post 与大型/复杂 json 数据集一起使用时,Internet Explorer 11 崩溃

css - 如何使生成的内容可选择?

html - 使元素与 float 元素的边框相邻

jquery - 为什么 $ ("div[id*='\\|']") 在 IE8 或 IE9 中不起作用?

jquery - 制作一个用户控件的 DLL 以及嵌入的 Jquery

internet-explorer-11 - ModernIE VM Windows 10 for Parallels 无法运行

CSS :after pseudo-element combined with [checked]: different after-elements not working?

javascript - jQuery获取文本框的值