【问题标题】:jQuery Find the specific text in the pagejQuery 查找页面中的特定文本
【发布时间】:2013-10-31 09:03:36
【问题描述】:

如果此文本存在“购物车为空”,我想将用户自动重定向到另一个页面。我找到了这个 jQuery :contains() 选择器并对其进行了测试,但它没有用。这是我的代码:

<div class="page-title">
<h1>Shopping Cart is Empty</h1>
</div>

<script>
 $( "div.page-title:contains('Shopping Cart is Empty')".window.location.replace("http://www.another-page.com");
</script>

【问题讨论】:

  • 您需要一个事件来调用您的“函数”,例如.load、.ready、.click
  • 您需要将其放在if else 语句中,正如@TimoJungblut 已经说过的,将其放在.ready 内或页面末尾。
  • @TimoJungblut 我不擅长 jQuery 事件调用,还在学习基础知识,你能帮我重新构建我的代码吗?
  • @kirk 用 DEMO 检查我的答案
  • @kirk palaSH 为你添加了一个很好的例子

标签: jquery redirect find


【解决方案1】:

你可以这样做:

// Call the function when DOM is ready
$(function () {

    // Check if page title has a text using length
    var len = $("div.page-title h1").filter(function () {
        return $(this).text() === "Shopping Cart is Empty";
    }).length;

    // If the text is there on the page, redirect to another page
    if (len > 0) {
        window.location.replace("http://www.another-page.com");
    }
});

【讨论】:

  • @palasH 我现在测试了,但仍然没有重定向到另一个页面
  • .length 不是.length()
【解决方案2】:

试试这个:

if($("div.page-title :contains(Shopping Cart is Empty)").length > 0){
    window.location.href = "http://www.another-page.com";
}

Fiddle here.

【讨论】:

    【解决方案3】:
    if($('.page-title h1')text()=="Shopping Cart is Empty"){
    

    window.location.replace("http://www.another-page.com"); }

    【讨论】:

      【解决方案4】:

      当然要确保你的代码在 jQuery 加载函数之类的里面。

      $(function(){
      
      ..Your Code here...
      })
      

      然后,您可以尝试使用indexOf() 更具体。

      类似:

      if( $("div.page-title h1").text().indexOf('Empty') !=-1 ){
      
      window.location.href="http://www.goodle.com";
      
      }
      

      所以,总而言之,它会是这样的:

      $(function(){
      
      if( $("div.page-title h1").text().indexOf('Empty') !=-1 ){
      
          window.location.href="http://www.goodle.com";
      
          }
      
      })
      

      希望对你有帮助!

      【讨论】:

        【解决方案5】:

        Fiddle DEMO

        $(function () {
            var len = $("div.page-title h1").filter(function () {
                return this.innerHTML === "Shopping Cart is Empty";
            }).length;
            if (len > 0) {
                window.location.replace("http://www.another-page.com");
            }
        });
        

        【讨论】:

        • 像魅力一样工作!谢谢!
        • @kirk 欢迎 乐于助人 :)
        • 这和@palaѕн 的回答有什么区别?
        • @Tushar 我可以再寻求帮助吗,这仍然与我上面的帖子有关。
        • @ErickBest 他使用$(this).text() 我使用this.innerHTML 更快,他使用.length() 但正确的是.length
        【解决方案6】:

        你也可以像这样使用contains

        <script>
            if ($( "div.page-title:contains('Shopping Cart is Empty')").length > 0) {
                window.location.replace("http://www.another-page.com");
            }
        </script>
        

        content 元素返回所有包含指定字符串的 divs 元素对象。所以检查长度然后重定向。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-06
          • 2010-11-15
          • 1970-01-01
          • 2011-05-05
          • 2016-09-07
          相关资源
          最近更新 更多