Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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 从另一页加载div后更改元素_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 从另一页加载div后更改元素

Javascript 从另一页加载div后更改元素,javascript,jquery,dom,Javascript,Jquery,Dom,我使用了$(“#header”).load(“index.html#header”)从index.html加载id为“header”的div,并在另一个名为current.html的页面中使用它来显示相同的div 在index.html上的头div(原始div)中,有一个id为“about”的元素。现在,在复制之后,我想更改“about”标记的类。但是,当我使用document.getElementById(“about”).className=“active”时,它返回null,因为这个div

我使用了
$(“#header”).load(“index.html#header”)
从index.html加载id为“header”的div,并在另一个名为current.html的页面中使用它来显示相同的div

在index.html上的头div(原始div)中,有一个id为“about”的元素
。现在,在复制之后,我想更改“about”标记的类。但是,当我使用
document.getElementById(“about”).className=“active”
时,它返回null,因为这个div被复制了

有人能帮忙吗

它返回null,因为此div已被复制

事实并非如此。我相信您只是试图在元素存在之前访问它。将回调传递到
.load
,并在加载HTML后更改类:

$("#header").load("index.html #header", function() {
    $('#about').addClass('active');
});

另请参见:。

那么,复制后是否在同一页面中有两次“#header”元素?您不应该在可以多次出现的元素中使用ID。你能举一个JSFIDLE的例子吗?另一件事是:如果我复制并超过这个div的所有cod,字体将是相同的,但是如果我使用load,字体(这个div中的所有内容)将变小。你也能帮忙吗?听起来你的CSS规则有问题。检查元件并检查适用的规则。