【问题标题】:this.id in jQuery won't fetch the element id if it is a number or contains numbers如果元素 id 是数字或包含数字,则 jQuery 中的 this.id 不会获取
【发布时间】:2009-04-01 01:59:01
【问题描述】:

我有一堆自动生成的 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,但未应用“可拖动”类。

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

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    请注意,纯数字 ID 无效。请参阅this post 了解更多信息。

    短而甜的是:

    ID 和 NAME 令牌必须以 字母 ([A-Za-z]) 并且可以跟随 由任意数量的字母、数字 ([0-9])、连字符 ("-")、下划线 ("_")、冒号 (":") 和句点 (".")。

    【讨论】:

    • 我会的。后期开发中。需要帮助吗?
    • @Steve,解决您的 ID 问题,您所询问的问题将消失(很可能)。 =] 一石两鸟。
    • 它不会,因为我已经这样测试过,问题仍然存在。
    • @Steve,那么肯定有其他问题。你能发布你的 HTML 吗?
    【解决方案2】:

    虽然允许使用句号和冒号,但我不推荐。

    如果你必须根据 id 进行选择,jQuery 会失败,因为它会认为这是一个新的类选择器或过滤器。

    【讨论】:

      【解决方案3】:

      在生成的 html 中添加如下前缀:

      <div id="prefix_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>
      

      并且在java脚本中,添加相同的前缀

      【讨论】:

        【解决方案4】:

        您正在尝试获取元素的 ID 以及完全覆盖它们的其他子元素...这就是为什么您没有获得任何 ID。

        试试 e.target.id,而不是 this.id。如果你点击 flash 电影,你会得到 id="flash"。

        http://jsfiddle.net/rzfPP/49/

        另外你定位错误的元素。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-30
          • 2019-01-12
          • 1970-01-01
          • 2016-04-19
          • 1970-01-01
          • 2012-11-02
          • 1970-01-01
          • 2015-07-21
          相关资源
          最近更新 更多