[英]changing id of div using jquery
编辑2:问题解决了。 jquery插件创建了一个div。 我正在吠叫错误的树。 谢谢你所有的快速答案! stackoverflow的长时间读者,但我第一次发布了一个问题!
编辑 :所以我想更改id的原因是更改评级栏的评级(我使用jrating jquery插件)。 插件使用id开头的数字来设置初始评级。 现在我想在加载新玩家时使用ajax并且新玩家有一个我想要更新的新评级。 这有足够的背景吗?
我在这里不知所措。 我想改变所选div
的id
,但它不起作用! 我似乎改变div id
,因为document.title
有正确的id
,当我给它的id
中的div
我只是改变了。 但是,当我打开我的网页的源代码时, id
没有改变...
function changePlayer() {
xmlhttp = createXHR();
xmlhttp.onreadystatechange = function () {
document.title = "onreadystatechange";
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.title = "4 and 200 is ok!";
var xmlDocument = xmlhttp.responseXML;
var playerId = xmlDocument.getElementsByTagName("id")[0].childNodes[0].nodeValue;
var playerName = xmlDocument.getElementsByTagName("firstName")[0].childNodes[0].nodeValue;
var playerlastName = xmlDocument.getElementsByTagName("lastName")[0].childNodes[0].nodeValue;
var playerTeamId = xmlDocument.getElementsByTagName("teamId")[0].childNodes[0].nodeValue;
var playerPicUrl = xmlDocument.getElementsByTagName("mainPic")[0].childNodes[0].nodeValue;
var playerShooting = xmlDocument.getElementsByTagName("shooting")[0].childNodes[0].nodeValue;
document.getElementById("playerNameAndTeam").innerHTML = "<b>" + playerName + " " + playerlastName + "</b>" + "<br>" + playerTeamId;
document.getElementById("playerPicture").src = "img/players/" + playerPicUrl;
$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting"); //attr("id", "5.1_shooting");
document.title = $("[id*='_shooting']").attr("id");
$("[id*='_shooting']").css("background-color", "blue");
$("[id*='playerNameA']").css("background-color", "blue");
}
}
xmlhttp.open("GET", "playerPageMVC/AJAX/getInfoPlayer.php", true);
xmlhttp.send();
}
function createXHR() {
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
} else { // code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
尝试使用Native JS,例如替换此行:
$("[id*='_shooting']").attr("id",playerShooting/10 + "_shooting");
通过这个:
$("[id*='_shooting']")[0].id = playerShooting/10 + "_shooting";
注意:如果$("[id*='_shooting']")
匹配许多元素,则只会更改第一个元素。
编辑:如果你想保持jQuery技术,取决于你的版本,你可以尝试使用prop(...)
而不是attr(...)
当我尝试使用JQuery使用示例代码更改ID时,它工作正常。 你可以在这里查看示例。 只需更改id attribute
您可以通过检查元素而不是通过检查加载的网页源来检查ID更改 。
如果你的意思是:
$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");
您只需执行以下操作即可检查该ID:
$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");
alert($("#"+ playerShooting / 10 + "_shooting").attr("id"));
通过查看源,您只能查看原始源,而不是当前状态中存在的DOM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.