jQuery .html() 在 ie7 中不显示任何数据,但 ie8 可以工作
那么谁没有遇到 ie7 的问题呢?
看来我无法让ie7识别一些数据。
我为我的客户创建了一个 WordPress 主题,这允许他们通过管理员在 wordpress 元字段
中添加额外信息,以便他们可以显示额外信息。
我编写了一个小的 jQuery 脚本,它查看图像 alt
和 title
来收集这些信息并将其写入 div 中。
下面是我的脚本。
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){
var title = jQuery('.attachment-post-thumbnail').attr('title');
var alt = jQuery('.attachment-post-thumbnail').attr('alt');
jQuery('#vehicle-alt').html('<h2 class="car-detail">'+title+'</h2><p>'+alt+'</p>');
});
//]]>
</script>
当我在ie7中查看源代码时,没有任何信息被传递。
div 就像 一样简单
So who doesn't have issues with ie7 ?
It seems that I cannot get ie7 to recognize some data.
I have created a wordpress theme for a client of mine, and this allows them to put in extra information in the wordpress meta fields
through the admin so they can display extra info.
I have written a small jQuery script that looks at an images alt
and title
to gather this info and write it into a div.
Below is my script.
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){
var title = jQuery('.attachment-post-thumbnail').attr('title');
var alt = jQuery('.attachment-post-thumbnail').attr('alt');
jQuery('#vehicle-alt').html('<h2 class="car-detail">'+title+'</h2><p>'+alt+'</p>');
});
//]]>
</script>
When i look at the source code in ie7, there is no information that has been passed.
the div is as simple as <div id="vehicle-alt"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.html() 方法使用浏览器的innerHTML 属性。某些浏览器可能无法生成完全复制所提供的HTML 源的DOM。
看起来 jQuery html() 方法调用了现有 DOM 对象上的empty() 方法。因此,在 div 上尝试使用empty()而不是html(),然后尝试append()。例如
,还请尝试 JQuery 提供的其他 dom 操作方法。
.html() method uses the browser's innerHTML property.Some browsers may not generate a DOM that exactly replicates the HTML source provided.
It seems that jQuery html() method calls the method empty() on an existing DOM object. Hence try empty() instead of html() on the div, and try append(). For example
Also please try other dom manipulation menthods provided from JQuery.
我也有这个错误。
解决方案是验证我尝试打印或添加的 html 内容。
我尝试了几个 jquery 函数:html、append、appendTo...但没有任何效果。
就我而言,html 有一个额外的关闭标签。我删除了它,现在一切正常。
I had this bug too.
The solution is to validate the html content that I was trying to print or add.
I tried several jquery functions: html, append, appendTo... But nothing worked.
In my case the html had an extra close tag. I deleted it and now everything is working ok.
当有两个或多个具有相同 ID 的元素时,我遇到了这个错误。仅在 IE7 中出现。
当然,解决方法是确保每个元素都有唯一的 ID。
I had this bug when there was two or more elements with the same ID. It occured only in IE7.
The fix was of course to make sure that each element had a unique ID.