我遇到了带有多个id的jquery模式的问题

时间:2014-07-19 09:42:17

标签: jquery html

我是jQuery的新手,我有来自jquery库的以下脚本。在单个页面上,我有多个链接,当用户点击它时,会弹出一个对话消息。到目前为止,这是我的代码。我的问题是,如果我在一个页面上有大约30个链接,我应该用不同的ID一遍又一遍地创建一个新的jquery函数吗?怎么样?

<script>
         $(function() {
        $( "#dialog1" ).dialog({
        autoOpen: false,
        show: {
        effect: "blind",
        duration: 1000
        },
        hide: {
        effect: "explode",
        duration: 1000
        }
        });
        $( "#opener1" ).click(function() {
        $( "#dialog1" ).dialog( "open" );
        });
        });

         $(function() {
        $( "#dialog2" ).dialog({
        autoOpen: false,
        show: {
        effect: "blind",
        duration: 1000
        },
        hide: {
        effect: "explode",
        duration: 1000
        }
        });
        $( "#opener2" ).click(function() {
        $( "#dialog2" ).dialog( "open" );
        });
        });
    </script>

我也有以下html

<div title="click to view details">
                <a href="#" class="p1" rel="nofollow" aria-haspopup="false" id="opener1"></a>
                    <p id="dialog1">
                        <strong>Monkey</strong>Monkeys are generally considered to be intelligent. Unlike apes, monkeys usually have tails.
                    </p>
                        <b class="b1"></b>
            </div>
            <div title="click to view details">
                <a href="#" class="p2" rel="nofollow" aria-haspopup="false" id="opener2"></a>
                    <p id="dialog2">
                        <strong>Vulture</strong>A particular characteristic of many vultures is a bald head, devoid of normal feathers.
                    </p>
                        <b class="b2"></b>
            </div>

1 个答案:

答案 0 :(得分:0)

您至少有两种方法可以完成此任务。 可能最好的方法是使用类,因此您在要管理的链接上使用相同的类,然后使用class selector将操作绑定到它们。 另一种方法是在a标记上使用each函数。我不认为在你的情况下这是最好的解决方案,但它只是说在某些情况下你也可以使用这个解决方案。