【问题标题】:Using one javascript for multiple elements [duplicate]对多个元素使用一个 javascript [重复]
【发布时间】:2013-03-13 22:22:04
【问题描述】:

对于弹出框,我正在使用此代码。我想添加多个元素。当我添加多个具有相同 ID 的元素时,脚本不起作用。它是如何解决的?脚本是。

<a href="#" id="button">Click me</a>
<div id="modal">
    <div id="heading">
        Are you sure you want to do that?
    </div>  
</div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.reveal.js"></script>

<script type="text/javascript">
        $(document).ready(function() {
                $('#button').click(function(e) { // Button which will activate our modal
                        $('#modal').reveal({ // The item which will be opened with reveal
                                animation: 'fade',                   // fade, fadeAndPop, none
                                animationspeed: 600,                       // how fast animtions are
                                closeonbackgroundclick: true,              // if you click background will modal close?
                                dismissmodalclass: 'close'    // the class of a button or element that will close an open modal
                        });
                return false;
                });
        });
</script>

我想这样添加。

<a href="#" id="button">Click me</a>
    <div id="modal">
        <div id="heading">
            Are you sure you want to do that?
        </div>  
    </div>
<a href="#" id="button">Click </a>
    <div id="modal">
        <div id="heading">
            You want to do that?
        </div>  
    </div>

【问题讨论】:

  • ID 必须是唯一的。改用类。

标签: javascript


【解决方案1】:

ID 是唯一的。该规范只允许每个 id 有一个元素。

改用类

<div class="toActOn">
        Are you sure you want to do that?
</div>  
<div class="toActOn">
        Really sure
</div>  

然后在 jQuery 中将选择器更改为 .toActOn (即 $(".toActOn")

这解释了根据the W3c是什么ID

ID 类型属性的特殊之处在于,在一个符合标准的文档中,没有两个这样的属性可以具有相同的值,无论携带它们的元素的类型如何;无论使用哪种文档语言,ID 类型属性都可用于唯一标识其元素。在 HTML 中,所有 ID 属性都被命名为“id”;

【讨论】:

  • 它工作,但弹出框的内容存在于窗口中。怎么解决的?
  • @Aneesh 很抱歉,我不明白您的要求。如果您想详细讨论类与 ids 的主题,或者如何解决您遇到的另一个特定问题,我认为这超出了这个问题的范围。非常欢迎您加入我的聊天 (chat.stackoverflow.com/rooms/17/javascript)。如果您对与此问题有关的同一问题感兴趣,如果您能更好地解释您遇到的问题,我将不胜感激。 “窗口中存在弹出窗口”我很难理解。
  • @Aneesh 如果您觉得我的回答解决了您的问题,请考虑接受。
【解决方案2】:

使用 class 而不是 id 来识别元素...

<a href="#" class="button" modal="m1">Click me</a>
<div id="m1">
    <div id="heading">
        Are you sure you want to do that?
    </div>  
</div>
<a href="#" class="button" modal="m2">Click</a>
<div id="m2">
    <div id="heading">
        Are you sure?
    </div>  
</div>
<a href="#" class="button" modal="m3">Click this</a>
<div id="m3">
    <div id="heading">
        Please confirm
    </div>  
</div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/jquery.reveal.js"></script>

<script type="text/javascript">
        $(document).ready(function() {
                $('.button').click(function(e) { // Button which will activate our modal
                        $('#' + $(this).attr('modal') + ').reveal({ // The item which will be opened with reveal
                                animation: 'fade',                   // fade, fadeAndPop, none
                                animationspeed: 600,                       // how fast animtions are
                                closeonbackgroundclick: true,              // if you click background will modal close?
                                dismissmodalclass: 'close'    // the class of a button or element that will close an open modal
                        });
                return false;
                });
        });
</script>

【讨论】:

  • 我尝试了这段代码并且工作正常,但是在两个按钮上单击显示相同的内容。像这样的代码 点我 谢谢
  • 我不确定你的意思。您想为每个按钮显示不同的内容吗?
  • 好的,我已经修改了答案以允许每个按钮有不同的内容。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 2014-09-02
  • 2014-07-11
  • 1970-01-01
  • 2021-10-12
相关资源
最近更新 更多