我有一堆自动生成的 div,它们的 id 有一个数字。每当我使用 this.id 获取 id 时,它不会输出任何内容。但是,当 ID 仅是文本时,它会正确输出 ID。我该如何解决这个问题?任何事情都有帮助。谢谢
编辑:
$(".draggable").draggable();
$("*", document.body).click(function (e) {
var offset = $(this).offset();
e.stopPropagation();
$("#result").text(this.tagName + " ID:" + this.id + " Coord:" + offset.left + " " + offset.top);
$.post("http://localhost/framework/test.php", "id=" + this.id + "&x=" + offset.left + "&y=" + offset.top);
});
这是有问题的代码。然而,它将获取 div 的 id,但不应用“draggable”类。
<div id="container">
<div id="1" class="draggable" style="top:55px; left:55px; border: 2px solid black; display: inline-table; position: relative; background-color: grey; max-width: 500px;">
<div>Im an Image Posted by:Teh_noob Posted on: 9/15/1991</div>
<div><img src="http://video.google.com/img/logo_video.gif?hl=en" /></div>
</div>
<div id="2" class="draggable" style="top:57px; left:33px; border: 2px solid black; display: inline-table; position: relative; background-color: grey; max-width: 500px;">
<div>Youtube test Posted by:teh nub Posted on: 9/11/01</div>
<div><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/_AJ0SkbPxAk&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><embed id="flash" wmode="transparent" src="http://www.youtube.com/v/_AJ0SkbPxAk&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowfullscreen="true" width="640" height="385"></embed></object></div>
</div>
<div id="3" class="draggable" style="top:33px; left:12px; border: 2px solid black; display: inline-table; position: relative; background-color: grey; max-width: 500px;">
<div>Text box Posted by:teh noob Posted on: 8/23/09</div>
<div>TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST</div>
</div>
</div>
<p id="result"></p>
最佳答案
请注意,纯数字 ID 无效。请参阅this post了解更多信息。
简短而甜蜜的是:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
关于javascript - 如果元素 id 是数字或包含数字,jQuery 中的 this.id 将不会获取元素 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/703774/