Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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
Javascript jQuery未将类添加到选定的div id_Javascript_Html_Jquery - Fatal编程技术网

Javascript jQuery未将类添加到选定的div id

Javascript jQuery未将类添加到选定的div id,javascript,html,jquery,Javascript,Html,Jquery,我正在网站上尝试这个代码,但它根本不起作用 <script src="https://code.jquery.com/jquery-3.5.0.js"></script> <script> var link = jQuery(location).attr('pathname'); var fullurl = jQuery(location).attr('href'); if (link.split("/")[3] ===

我正在网站上尝试这个代码,但它根本不起作用

<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
var link = jQuery(location).attr('pathname');
var fullurl = jQuery(location).attr('href');
if (link.split("/")[3] === "sardines") {
    console.log("fullurl = " + fullurl);
    console.log("pathname = " + link);
    console.log("loop sardines");
    jQuery('#sardines').addClass('capik');
    console.log("end addClass");
}
else if (link.split("/")[3] === "saba"){
    console.log("loop saba");
    jQuery('#saba').addClass('capik');
    console.log("end addClass");
}
else{
    console.log("end of loop");
}

有人能帮我解决这个问题吗。脚本在站点上运行良好,但是jQuery没有在所选id上添加任何类。循环也运行良好。如何让jQuery在所选的div id上添加新类。这里我附上控制台的屏幕截图,显示脚本运行正常,以及html的屏幕截图。但是该类没有添加到所选的类中。看起来它跳过了那里的addClass脚本

这就是我在沙丁鱼页面上的时候。

用jQuerydocument.ready包装代码。您的代码可能是在html完全加载之前执行的。表示您的选择器在元素尚不存在时被执行

// document.ready
jQuery(document).ready(function () {
    var link = jQuery(location).attr('pathname');
    console.log("url = " + link);
    console.log(link.split("/")[3]);
    console.log(link.split("/")[3] === "sardines");
    if (link.split("/")[3] === "sardines") {
        console.log("loop sardines");
        jQuery('#sardines').addClass('class');
        console.log("end addClass");
    }
    else if (link.split("/")[3] === "saba"){
        console.log("loop saba");
        jQuery('#saba').addClass('class');
        console.log("end addClass");
    }
    else{
        console.log("end of loop");
    }
})

可以添加位置和路径名的值吗?这将更容易发现问题。我只需更新console@ROOT,您可以在这里查看开发页面。
// document.ready
jQuery(document).ready(function () {
    var link = jQuery(location).attr('pathname');
    console.log("url = " + link);
    console.log(link.split("/")[3]);
    console.log(link.split("/")[3] === "sardines");
    if (link.split("/")[3] === "sardines") {
        console.log("loop sardines");
        jQuery('#sardines').addClass('class');
        console.log("end addClass");
    }
    else if (link.split("/")[3] === "saba"){
        console.log("loop saba");
        jQuery('#saba').addClass('class');
        console.log("end addClass");
    }
    else{
        console.log("end of loop");
    }
})