【问题标题】:Using jQuery to replace each ' with &apos使用 jQuery 将每个 ' 替换为 &apos
【发布时间】:2009-06-05 19:56:01
【问题描述】:

我似乎找不到正确的方法来获取从服务器返回的一些 html,将每个 ' 替换为 &apos,最后 - 将新的 html 附加到 DOM。

在修改之前,以下内容可以很好地附加 html(未更改)

$(xhtml).find("#inner").appendTo("#appendTD");

但现在我想先更改 xhtml,下面的内容似乎无法按预期工作

var html = $(xhtml).find("#gridFormInformation").find("'").replaceWith("'");
$(html).find("#inner").appendTo("#appendTD");

从上面可以看出,我想用 ID #gridFormInformation 替换表格元素中的每个 ' 和 &。这部分不起作用,因为我试图附加的 html obj 是一个 jQuery 对象(不是我在第一个工作示例中附加的简单字符串)

有什么帮助吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:
    xhtml.replace(/'/g, ''');
    $(xhtml).find("#inner").appendTo("#appendTD");
    

    findreplaceWith 方法不适合在您使用它们时使用。它们是基于 CSS 选择器查找和替换元素的 jQuery 函数。您需要使用字符串的原生替换功能,然后执行jQuery。

    【讨论】:

    • 其实我觉得你需要 xhtml.replace(/'/g, ''');
    【解决方案2】:

    find() 需要一个 CSS 有效的表达式,而 "'" 不是。尝试使用替换()。 http://www.w3schools.com/jsref/jsref_replace.asp

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 2010-11-24
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 2010-11-18
      相关资源
      最近更新 更多