我正在开发一个项目来移动div元素并更新其id。 它调用脚本并传递标签的id号。到目前为止它完全有效。它将元素向上移动一个位置并交换div id数字。唯一的事情就是当我点击相同的div再次移动它时(比如通过第二次点击“向上”将div#3移动到#1点),它不会响应新的div“id”,即使当我在浏览器中检查它时,它似乎已成功更改。
javascript:只显示if up
enter code here
$("#content_update").on("click", ".rowupdown", function(){
// button is up or down
var upordown = $(this).attr('value');
// id number of div from button
var number = $(this).attr('id');
// current div can edit id
var $moveid = $("div#row" + number);
var numberint = parseInt(number);
if (upordown == "up") {
var s = numberint - 1;
//number of div to place before and to set id with
var numberstring = s.toString();
//***ability to select the other div element***
var $diffrow = "div#row" + numberstring;
//** update current div id***
var $editcurrent = "div#row" + number;
// puts other div id into this one
var newid = "row" + numberstring;
// puts in this id to the other div
var editother = "row" + number;
$($diffrow).before($moveid); // finally moves the current div up one
$($diffrow).attr("id", editother);
$($editcurrent).attr("id", newid); //changes id of current div to one before
}
});
HTML(我有一个添加新div的按钮,并添加1给他们一个唯一的ID,第一次点击= 1秒使div id = 2等等,但它与问题无关。 )
case "row":
if (!isset($_SESSION["rownumber"])){
$row = $_SESSION["rownumber"] = 1;
}
$row = $_SESSION["rownumber"]++;
echo " the html
<div class='panel panel-default' id='row".$row."'>
<button class='rowupdown point' id='".$row."' value='up'>up</button>
</div>