jQuery无法在Internet Explorer上读取自定义标签文本 [英] Jquery cannot read custom tag text on Internet Explorer

查看:112
本文介绍了jQuery无法在Internet Explorer上读取自定义标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML页面中使用自定义标记,并使用jquery读取其中的文本, 像这样的东西:

<desc>Hello world</desc>

然后用jquery读取文本:

desc_text=$("desc").first().text();

在firefox和Chrome上,它可以工作,我得到了"Hello world",但没有使用Internet Explorer(IE 9).

调试后,看来问题出在IE上:<desc></desc>被视为两个不同的DOM节点. Hello world不是<desc>的子节点. 这就是它不起作用的原因.如果我使用span标记而不是desc标记:它将起作用.

有没有办法使它在IE上运行,还是IE不解释我的自定义标签(如firefox/Chrome)是正常的?

解决方案

只需在文档的开头添加

<script>document.createElement('desc');</script>

这也是HTML5shiv所采用的基本思想,以使IE< 9能够正确解析新的HTML5元素(无论如何,您将无法通过javascript将这个元素注入到DOM中) /p>

I would like to use custom tag into my html page and use jquery to read the text in it, something like :

<desc>Hello world</desc>

then with jquery I read the text with :

desc_text=$("desc").first().text();

On firefox and Chrome, it works, I got the 'Hello world', but not with Internet Explorer (IE 9).

After debugging, it seems that the problem comes from IE : <desc> and </desc> are seen as two different DOM nodes. The Hello world is not a child node of <desc>. That's the reason it does not work. If I use the span tag instead of the desc tag : it works.

Is there a way to make it works on IE, or is that normal that IE does not interpret my custom tag like firefox / Chrome ?

解决方案

just add on the head of your document

<script>document.createElement('desc');</script>

this is the basic idea also adopted from HTML5shiv to make IE<9 able to properly parse the new HTML5 elements (anyway be aware that you won't be able to inject dinamycally this element via javascript into the DOM)

这篇关于jQuery无法在Internet Explorer上读取自定义标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆