Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.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/5/spring-mvc/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
Javascript-操作和使用此变量_Javascript - Fatal编程技术网

Javascript-操作和使用此变量

Javascript-操作和使用此变量,javascript,Javascript,我正在尝试使用这个函数来更改另一个元素的类和onclick partner = this.id + 'Content'; document.getElementById(partner).className = 'newswrapper'; document.getElementById(partner).onclick = function(){ clickNews(this); } ; 带有onclick的元素的id如下所示 echo'&l

我正在尝试使用这个函数来更改另一个元素的类和onclick

partner = this.id + 'Content';

            document.getElementById(partner).className = 'newswrapper';
            document.getElementById(partner).onclick = function(){ clickNews(this); } ;
带有onclick的元素的id如下所示

echo'<div id="newsSlide'.$counter.'" class="newsimage" 
            onclick="clickNews(this)" 
            style='."'".'background-image:url("../img/'.$row['image'].'")'".'>';

echo'您缺少
文档。

document.getElementById(partner).className = 'newswrapper';

getElementById
是文档的方法,而不是窗口;)

您缺少
文档。

document.getElementById(partner).className = 'newswrapper';

getElementById
是文档的方法,而不是窗口;)

partner=this.id+'Content'
中,您的
this.id
是指窗口对象上的全局变量id,而不是元素的id,因为没有全局变量,所以
partner
变量看起来像
undinedcontent

因此,更改
onclick=“clickNews(this.id)”
并使用参数中的id

function clickNews(id) 
    {
        partner = id + 'Content'; // use id form arguments
    }

举个简单的例子,
partner=this.id+“Content”
中的
this.id
是指窗口对象上的全局变量id,而不是元素的id,因为没有全局变量,
partner
变量看起来像
undinedcontent

因此,更改
onclick=“clickNews(this.id)”
并使用参数中的id

function clickNews(id) 
    {
        partner = id + 'Content'; // use id form arguments
    }

通过一个简单的例子

这里是我测试函数的地方,它仍然不能工作。获取错误Uncaught TypeError:无法设置null的属性'className',除了最后一个之外,所有这些都可以工作。这表明最后一个没有相应的元素。这里是我测试函数的地方,它仍然不工作。获取错误Uncaught TypeError:无法设置null的属性'className',除最后一个之外,所有这些函数都工作。这表明最后一个没有相应的元素。解决方案?将onclick更改为onclick=“clickNews(this.id)”。然后调整函数以使用id?????对不起,你能给我介绍更多的解决方案吗?我一直在整理我的PHP能力,但我仍然在学习javascriptI,我会摆弄简单的示例解决方案?将onclick更改为onclick=“clickNews(this.id)”。然后调整函数以使用id?????对不起,你能给我提供更多的解决方案吗?我一直在整理我的PHP能力,但我仍然在学习javascriptI,我对简单的示例很感兴趣