使用jquery更改div id

时间:2022-12-07 21:07:43

EDIT 2: Problem solved. The jquery pluggin created a div. I was barking up the wrong tree. Thanks for all your swift answers! long time reader of * but first time i've posted a question!

编辑2:问题解决了。jquery pluggin创建了一个div。谢谢你迅速的回答!*的长期读者,但是我第一次发布问题!

EDIT: So the reason why I want to change the id is to change the rating of a rating bar(I use the jrating jquery pluggin). The pluggin uses the digits in the beginning of the id to set the initial rating. Now I want to use ajax when I load a new player and that new player has a new rating that I want to update. Is this enough context?

编辑:所以我想要更改id的原因是要更改评级条的评级(我使用jrating jquery pluggin)。pluggin使用id开头的数字设置初始评级。现在我想要使用ajax当我加载一个新播放器时那个新播放器有一个我想要更新的新评级。这是足够的背景吗?

Im at a loss here. I want to change the id of a selected div but it doesnt work! I does seem to change the div id because document.title has the correct id when I give it the id of the div I just changed. However When I open the source code of my webpage the id didnt change...

我在这里不知所措。我想要更改所选div的id,但它不起作用!我似乎因为文档而改变了div id。title有正确的id当我给它我刚刚修改的div的id。然而,当我打开网页的源代码时,我的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");
    }
}

3 个解决方案

#1


1  

Try with Native JS like replacing this line :

尝试使用本地JS来替换这行:

$("[id*='_shooting']").attr("id",playerShooting/10 + "_shooting");

By this one :

通过这一个:

$("[id*='_shooting']")[0].id = playerShooting/10 + "_shooting";

Caution : If $("[id*='_shooting']") match many elements, only the first will be changed.

注意:如果$(“[id*='_shooting'])匹配多个元素,则只更改第一个元素。

EDIT : If you want to keep jQuery technic, depends on your version, you can try using prop(...) instead of attr(...)

编辑:如果你想保留jQuery技术,那就看你的版本了,你可以试试用道具(…)代替attr(…)

#2


1  

when I tried changing ID with a sample code using JQuery it worked. You can view example here. Just change the id attribute

当我尝试用JQuery示例代码更改ID时,它成功了。您可以在这里查看示例。只需更改id属性

You are suppose to check the id change by inspecting element not by checking the loaded source of the web page.

您应该通过检查元素而不是检查web页面的加载源来检查id更改。

#3


1  

If you mean your:

如果你的意思是:

$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");

You can check that id by simply doing these:

您可以通过以下操作检查id:

$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");
alert($("#"+ playerShooting / 10 + "_shooting").attr("id"));

By viewing the source, you only view the original source, not the DOM as it exists in a current state.

通过查看源文件,您只能查看原始源文件,而不能查看当前状态下的DOM。

#1


1  

Try with Native JS like replacing this line :

尝试使用本地JS来替换这行:

$("[id*='_shooting']").attr("id",playerShooting/10 + "_shooting");

By this one :

通过这一个:

$("[id*='_shooting']")[0].id = playerShooting/10 + "_shooting";

Caution : If $("[id*='_shooting']") match many elements, only the first will be changed.

注意:如果$(“[id*='_shooting'])匹配多个元素,则只更改第一个元素。

EDIT : If you want to keep jQuery technic, depends on your version, you can try using prop(...) instead of attr(...)

编辑:如果你想保留jQuery技术,那就看你的版本了,你可以试试用道具(…)代替attr(…)

#2


1  

when I tried changing ID with a sample code using JQuery it worked. You can view example here. Just change the id attribute

当我尝试用JQuery示例代码更改ID时,它成功了。您可以在这里查看示例。只需更改id属性

You are suppose to check the id change by inspecting element not by checking the loaded source of the web page.

您应该通过检查元素而不是检查web页面的加载源来检查id更改。

#3


1  

If you mean your:

如果你的意思是:

$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");

You can check that id by simply doing these:

您可以通过以下操作检查id:

$("[id*='_shooting']").attr("id", playerShooting / 10 + "_shooting");
alert($("#"+ playerShooting / 10 + "_shooting").attr("id"));

By viewing the source, you only view the original source, not the DOM as it exists in a current state.

通过查看源文件,您只能查看原始源文件,而不能查看当前状态下的DOM。