Javascript 如果p:包含字符串,则通过jQuery或重定向构建新页面

Javascript 如果p:包含字符串,则通过jQuery或重定向构建新页面,javascript,jquery,html,conditional-statements,Javascript,Jquery,Html,Conditional Statements,我有一个tumblr博客。缺点之一是它们不允许您自定义404错误页面。因为我是jQuery新手,所以我花了相当多的搜索和阅读时间才把这些行记下来 <script type="text/javascript"> $(document).ready(function() { $("p:contains(The URL you requested could not be found.)").closest('body').remove();

我有一个tumblr博客。缺点之一是它们不允许您自定义404错误页面。因为我是jQuery新手,所以我花了相当多的搜索和阅读时间才把这些行记下来

<script type="text/javascript">
        $(document).ready(function() {
            $("p:contains(The URL you requested could not be found.)").closest('body').remove();

        });
</script>
但该函数在每个页面上都会触发,无论是否出现错误404。

$()将返回一个数组。因为您必须准确地询问数组$()中是否有任何元素-property
length
包含数组中的元素数,我们必须检查它是否大于0
if($("p:contains(The URL you requested could not be found.)").length>0) {
                  window.location.href='newurl';
if($("p:contains(The URL you requested could not be found.)").length>0) {
                  window.location.href='newurl';