使用 id 选择器的 jQuery 和 ie8 似乎不起作用(使用 1.5.1)
我正在创建一个推荐旋转器,用于解析充满推荐的 XML 文件。每个推荐都被分配一个“类”,以便页面可以指定要使用的推荐类(例如“公司”或“高尔夫”并显示相关参考。)
问题是在 IE8 中(即使包含兼容性元 ) ) 特定 ID 内的 html 不会更改。我切换到课程以确保 Chrome 可以正常工作(Firefox 始终可以正常工作。) 下面是使用类的示例:
$("span.quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("span.attrib").html("- " + $(this).find("name").text() + "");
$("span.attrib").append(", " + $(this).find("title").text() + "");
$("span.attrib").append(", " + $(this).find("organization").text() + "<br />");
这些在 Firefox 和 Chrome 中工作。 下面使用 ID 选择器的代码仅适用于 Firefox,但这是我的首选方法。
$("#quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("#attrib").html("- " + $(this).find("name").text() + "");
$("#attrib").append(", " + $(this).find("title").text() + "");
$("#attrib").append(", " + $(this).find("organization").text() + "<br />");
我的 HTML 片段:
<span id="quote">
"This is the default quote."
</span>
<span id="attrib">
- Default name, default title, default organization
</span>
这段代码中有什么不正确的地方,导致 IE8 无法正确交换引号?当我将代码从选择器切换到警报时(只是为了确保解析循环正常工作),一切正常。
如有任何意见,我们将不胜感激。这里不知所措。返回 .NET 或 Flash 项目,让这个过程静置几个小时。
Rob
添加了更多代码示例:
testimonialflipper_tst.js 的内容
var filter_value = new String();
$(document).ready(readfilterXML("golf"));
function readfilterXML(passed_value)
{
filter_value = passed_value;
$.ajax({
type: "GET",
url: "testimonials.xml",
dataType: "xml",
success: parseXml
});
}
function parseXml(xml)
{
var holdclass = filter_value;
// need to seek class within id "quote" like "#quote.class" to find out what it is
//find every testimonial and load the array
$(xml).find("testimonial").each(function()
{
var tempclass = $(this).attr("class");
// $("#error").html(tempclass);
if (tempclass == holdclass) {
$("span#quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("span#attrib").html("- " + $(this).find("name").text() + "");
$("span#attrib").append(", " + $(this).find("title").text() + "");
$("span#attrib").append(", " + $(this).find("organization").text() + "<br />");
}
});
}
I'm creating a testimonial rotator that parses an XML file full of testimonials. Each testimonial is assigned a "class" so that pages can specify a class of testimonial to use (e.g. "corporate" or "golf" and have a relevant reference show up.)
The problem is that in IE8 (even with the compatibility meta included) the html within the specific ID's is not changed. I switched to classes to ensure that Chrome worked (Firefox always works.)
Below is an example using classes:
$("span.quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("span.attrib").html("- " + $(this).find("name").text() + "");
$("span.attrib").append(", " + $(this).find("title").text() + "");
$("span.attrib").append(", " + $(this).find("organization").text() + "<br />");
These work in Firefox and Chrome.
The code below using the ID selector works only in Firefox, but is my preferred method.
$("#quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("#attrib").html("- " + $(this).find("name").text() + "");
$("#attrib").append(", " + $(this).find("title").text() + "");
$("#attrib").append(", " + $(this).find("organization").text() + "<br />");
My HTML snippet:
<span id="quote">
"This is the default quote."
</span>
<span id="attrib">
- Default name, default title, default organization
</span>
What is incorrect in this code that IE8 won't correctly swap the quotes out? When I switch the code from the selectors to alerts (just to make sure the parsing loop works) everything works fine.
Any comments are appreciated. At a loss here. Going back to a .NET or Flash project to let this sit a few hours.
Rob
Added more code example:
contents of testimonialflipper_tst.js
var filter_value = new String();
$(document).ready(readfilterXML("golf"));
function readfilterXML(passed_value)
{
filter_value = passed_value;
$.ajax({
type: "GET",
url: "testimonials.xml",
dataType: "xml",
success: parseXml
});
}
function parseXml(xml)
{
var holdclass = filter_value;
// need to seek class within id "quote" like "#quote.class" to find out what it is
//find every testimonial and load the array
$(xml).find("testimonial").each(function()
{
var tempclass = $(this).attr("class");
// $("#error").html(tempclass);
if (tempclass == holdclass) {
$("span#quote").html(""" + $(this).find("quote").text() + ""<br />");
//print each testimonial name, title, and org if exists
$("span#attrib").html("- " + $(this).find("name").text() + "");
$("span#attrib").append(", " + $(this).find("title").text() + "");
$("span#attrib").append(", " + $(this).find("organization").text() + "<br />");
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有多个具有相同 ID 的
?如果你的 html 中有多个引号,这些 id 看起来很通用。
如果您确实有多个相同的 ID,则每个浏览器都会以不同的方式出现故障。
PS 更多代码会有所帮助。
Do you have more than one
<span>
with the same id? These id's seem pretty generic if you are going to have multiple quotes in your html.If you do have multiple identical id's, each browser will break in a different way.
P.S. More code would be helpful.