【问题标题】:jQuery Code is working on Google Chrome but not Firefox,IE and even SafarijQuery Code 适用于 Google Chrome,但不适用于 Firefox、IE 甚至 Safari
【发布时间】:2010-01-13 20:52:17
【问题描述】:

我编写了这段代码,它在 Google Chrome 中运行良好,但在 Firefox 和其他主流浏览器中,它甚至没有任何改变:

jQuery("div[style*='line-height:25px']").contents(":not(nodeType!='1',br)").wrap("<div style='margin-bottom:9px;line-height:normal;margin-top:4px'>");
jQuery("div[style*='line-height:25px'] br").remove();

这可能看起来有点奇怪,但这是我想要解决的一个非常令人沮丧的问题,所以无论如何让我给你关于系统的规格:

  • jQuery 1.3.2 使用noConflict() 方法避免与原型$ 冲突
  • Scriptaculous 1.7.1_beta3
  • 原型 1.5.1
  • 是的,我正在使用 jQuery(document).ready() 函数在 DOM 准备好后执行操作。

这些也是嵌入在页面中的库。

推荐: 我没有开发这个页面和其他数百个类似的页面,问题是它们都是静态页面并且使用shtml 至少共享一些通用代码。但是我不能删除任何这些库,因为这意味着我将不得不编辑很多页面,这将花费我数周的时间。所以实际上我正在寻找的是像上面这样的临时解决方案。

提前致谢。

部分 HTML :

<div style="font-size: 13px; line-height: 25px;">
  <!-- BULLETS MORE -->
  <div style="line-height: normal;">
    Fine quality, full grain pebble leather
  </div>
   Smooth Classic Napa leather construction
  <br />
   Lateral footprint with top access
  <br />
   Durable belt clip
  <br />
   Top flap with snap closure for added security
  <br />
   Soft velvet lining with light protective layer
  <br />
   Bottom push-through cutout for easy Motorola Droid removal
  <br />
   Simple Scandinavian rounded design
  <br />
   Sena craftsmanship and quality
  <br />
</div>

【问题讨论】:

  • 你能给我们完整的 HTML 输出(审查内容/在必要时排除图像),以便我们使用它吗?

标签: jquery firefox google-chrome


【解决方案1】:

稍微重写一下怎么样?

jQuery("div").filter(lineHeight).contents(nonElementNodes).wrap("<div style='margin-bottom:9px;line-height:normal;margin-top:4px'>");

jQuery("div").filter(lineHeight).find("br").remove();

function lineHeight() {
  return this.style.lineHeight == "25px";
}

function nonElementNodes() {
  return this.nodeType !== 1 || this.tagName !== 'BR';
}

【讨论】:

    【解决方案2】:

    我认为您的内容选择器设置不正确。你应该这样做:

    jQuery("div[style*='line-height:25px']").contents(":not(br)").filter(function(){return this.nodeType != 1;}).wrap("<div style='margin-bottom:9px;line-height:normal;margin-top:4px'>");
    jQuery("div[style*='line-height:25px'] br").remove();
    

    【讨论】:

    • 它也没有用。并且nodeType=1 允许与 Selector 字符串一起使用。这是一个例子:tinyurl.com/ycanhl2
    【解决方案3】:

    不要在选择器中使用',只需:

    jQuery("div[style*=line-height:25px]")
    

    【讨论】:

    • 只是让你知道,这不是解决方案,因为即使是 jQuery 也推荐使用 ' 并且它不会以这种方式工作:)
    【解决方案4】:

    哎呀。 jquery 可以让你编写一些非常强大的查询,但这并不意味着你应该这样做。

    如果你可以控制 html,你应该在需要的地方添加一些类,并用一些类选择器替换那些乱七八糟的东西,它可以在所有浏览器中工作并且启动速度更快

    【讨论】: