jQuery中的this.id不会获取元素ID,如果它是一个数字或包含数字 [英] this.id in jQuery won't fetch the element id if it is a number or contains numbers

查看:90
本文介绍了jQuery中的this.id不会获取元素ID,如果它是一个数字或包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆自动生成的div,其ID为数字。每当我使用this.id获取id时,它什么都不输出。但是,如果Id仅为文本,则会正确输出标识。我怎么能解决这个问题?任何帮助。谢谢

编辑:

  $(。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 =1class =draggablestyle =top:55px; left:55px; border:2px solid black; display:inline-table; position:relative; background-颜色:灰色;最大宽度:500px;>
< div> Im an图片发布者:Teh_noob发布于:9/15/1991< / div>

< div>< img src =http://video.google.com/img/logo_video.gif?hl=en/>< / div>
< / div>

< div id =2class =draggablestyle =top:57px; left:33px; border:2px solid black; display:inline-table; position:relative; background-颜色:灰色;最大宽度:500px;>
< div> Youtube测试发布者:teh nub发布于:9/11/01< / div>
< div>< object width =640height =385>< param name =movievalue =http://www.youtube.com/v/_AJ0SkbPxAk&hl= en& fs = 1& rel = 0>< / param>< param name =allowFullScreenvalue =true>< / param>< embed id =flashwmode =transparent src =http://www.youtube.com/v/_AJ0SkbPxAk&hl=zh-TW&fs=1&rel=0type =application / x-shockwave-flashallowfullscreen =truewidth =640高度= 385 >< /嵌入>< /对象>< / DIV>
< / div>

< div id =3class =draggablestyle =top:33px; left:12px; border:2px solid black; display:inline-table; position:relative; background-颜色:灰色;最大宽度:500px;>
< div>文本框发布者:teh noob发布于:8/23/09< / div>

< div> TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST< / div>
< / div>

< / div>
< p id =result>< / p>


解决方案

请注意,仅限数字的ID无效。有关更多信息,请参阅此文章



简单又甜蜜的是:


ID和NAME标记必须开始([A-Za-z]),可以通过任意数量的字母,数字
([0-9]),连字符( - )跟随
,下划线
(_),冒号(:)和句点
(。)。



I have a bunch of automatically generated divs that have a number for their id. Whenever I use this.id to fetch the id it outputs nothing. However when the Id is text only it outputs the id properly. How could I fix this? Anything Helps. Thanks

Edit:

$(".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);
            });

This is the offending code. It will get the id of divs however, with the "draggable" class not applied.

   <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>

解决方案

Note that numeric-only IDs are invalid. See this post for more info.

The short and sweet of it is:

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 (".").

这篇关于jQuery中的this.id不会获取元素ID,如果它是一个数字或包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆