Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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 id类_Javascript_Jquery_Css - Fatal编程技术网

Javascript 更新特定的div id类

Javascript 更新特定的div id类,javascript,jquery,css,Javascript,Jquery,Css,我没有成功地更改id的类。代码中的其他函数正在工作。我想在单击div时添加一个额外的css类 html html html 功能添加座椅(str){ 如果(str==“”){ document.getElementById(“Winkelmand”).innerHTML=“”; 返回; }否则{ if(window.XMLHttpRequest){ //IE7+、Firefox、Chrome、Opera、Safari的代码 xmlhttp=新的XMLHttpRequest(); }否则{ //

我没有成功地更改id的类。代码中的其他函数正在工作。我想在单击
div
时添加一个额外的css类

html
html
html
功能添加座椅(str){
如果(str==“”){
document.getElementById(“Winkelmand”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“Winkelmand”).innerHTML=xmlhttp.responseText;
}
};
open(“GET”、“winkelmand.php?q=“+str,true”);
xmlhttp.send();
$(document.getElementById(str.className)+='myseat';
}
}

document.getElementById不是jQuery函数,但您使用的是用于jQuery的$-符号

因此,要么删除$符号,要么使用Arun p Johny的第二个建议:

$('#' + str).addClass('myseat')

也许这个JSFiddle将有助于:

因为您已经有了JQuery,所以我冒昧地将您以前的AJAX代码转换为jQueryAjax,它更简洁。它还使用Arun P Johny建议的addClass()方法将该类添加到DOM元素中。显然,在这个JSFIDLE中,AJAX请求实际上不会工作,因为winkelmand.php不可用,但是如果您将代码复制到自己的站点,它应该可以工作

简言之,答案是:

$('#'+str).addClass('myseat');

正如Arun p Johny所建议的那样。

document.getElementById(str).className+='myseat'因为您有jQuery
$('#'+str).addClass('myseat')
document.getElementById(str).classList.add('myseat')
document.getElementById(str).classList.add(CLASS\u NAME)
谢谢。我试了很多,现在看起来很容易:-)