Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery在页面中查找特定文本_Jquery_Redirect_Find - Fatal编程技术网

jQuery在页面中查找特定文本

jQuery在页面中查找特定文本,jquery,redirect,find,Jquery,Redirect,Find,如果此文本存在“购物车为空”,我想将用户自动重定向到另一个页面。我找到了这个jQuery:contains()选择器并对其进行了测试,但它不起作用。这是我的密码: <div class="page-title"> <h1>Shopping Cart is Empty</h1> </div> <script> $( "div.page-title:contains('Shopping Cart is Empty')".window.l

如果此文本存在“购物车为空”,我想将用户自动重定向到另一个页面。我找到了这个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>

购物车是空的
$(“div.page-title:contains('购物车为空')”。window.location.replace(“http://www.another-page.com");

试试这个:

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

if($("div.page-title :contains(Shopping Cart is Empty)").length > 0){
    window.location.href = "http://www.another-page.com";
}
您可以执行以下操作:

// 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");
    }
});
您可以这样做:

// 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");
    }
});
窗口。位置。替换(“”); }

窗口。位置。替换(“”);
}

您也可以像这样使用
contains

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

if($(“div.page-title:contains('购物车为空'))。长度>0){
window.location.replace(“http://www.another-page.com");
}

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

您也可以像这样使用
contains

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

if($(“div.page-title:contains('购物车为空'))。长度>0){
window.location.replace(“http://www.another-page.com");
}

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

当然,请确保代码位于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";

    }

})
因此,总的来说,它将是这样的:

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";

    }

})

希望有帮助!

当然要确保代码在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";

    }

})
因此,总的来说,它将是这样的:

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";

    }

})


希望有帮助!

您需要一个调用“函数”的事件例如,.load、.ready、.click您需要将其放入
if else
语句中,正如@TimoJungblut已经说过的,将其放入
.ready
或页面末尾。@TimoJungblut我不擅长jQuery事件调用,仍在学习基本知识,您能帮我重新构建代码吗?@kirk检查我的答案DEMO@kirk帕拉什补充道一个很好的例子你需要一个调用你的“函数”的事件例如,.load、.ready、.click您需要将其放入
if else
语句中,正如@TimoJungblut已经说过的,将其放入
.ready
或页面末尾。@TimoJungblut我不擅长jQuery事件调用,仍在学习基本知识,您能帮我重新构建代码吗?@kirk检查我的答案DEMO@kirk帕拉什补充道一个很好的例子you@palasH我现在测试了它,但仍然没有重定向到另一页
。length
不是
。length()
@palasH我现在测试了它,但仍然没有重定向到另一页
。length
不是
。length()
很有魅力!谢谢你!@kirk欢迎帮助:)这和@palaѕѕѕѕ嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤220!谢谢大家!@柯克欢迎乐意帮忙:)这和@palaѕѕѕ嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤嘤