如何使用类而不是id来打开jQuery对话框?

3

我有一个页面上有很多链接,我只需要打开几个jQuery对话框。 如何使用类而不是id打开它们?

以下是我的脚本:

    <script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;
    $(function() {
        $( "#selectFolder" ).dialog({position:['middle',60],        
            open: function(event, ui) {  
            jQuery('.ui-dialog-titlebar-close').removeClass("ui-dialog-titlebar-close").html('<span style="float:right;"><img src="../images/x.png" /></span>');  
        },  
            dialogClass: 'ui-widget-shadow',
            modal: true,    
            autoOpen: false,
            width: '650px',
            close: function(ev, ui) {$(this).close();}      
        });

        $( "#selectFolderOpen" ).click(function() {
            $( "#selectFolder" ).dialog( "open" );
            return false;
        });
    });
    </script>
<div style="display:none;">
    <div id="selectFolder" title="Select Folder">
        <div style="display:block;">
            <!--#include file="sidebar_modal_questions_folder_select.asp"-->
        </div>
    </div>
</div>

这里有一个目前有效的例子:

<a href="#" class="buttonintable" id="selectFolderOpen">Select Folder</a>

我希望它能像这样工作:
<a href="#" class="buttonintable selectFolderOpen">Select Folder</a>

那样我就不必为每个要打开的链接都设置ID。我知道你使用('#selector')来表示ID,而('.selector')表示类 - 但我无法让它起作用。需要帮助吗?
3个回答

2

将您的选择器更改为$('.selectFolderOpen').click(...)

jQuery选择器可以选择任何可以在CSS选择器中定位的内容。它使用#来表示id,.(点)来表示class。


1
这是一个例子:http://jsfiddle.net/WVVXy/
$(function () {
  $("a.buttonintable").click(function () {
    $(this).dialog();
    return false;
  });
});

0

我回答了自己的问题。这是可行的代码:

<script>
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
    $( "#selectFolder" ).dialog({position:['middle',60],        
        open: function(event, ui) {  
        jQuery('.ui-dialog-titlebar-close').removeClass("ui-dialog-titlebar-close").html('<span style="float:right;"><img src="../images/x.png" /></span>');  
    },  
        dialogClass: 'ui-widget-shadow',
        modal: true,    
        autoOpen: false,
        width: '650px',
        close: function(ev, ui) {$(this).close();}      
    });

    $( "a.selectFolderOpen" ).click(function() {
        $( "#selectFolder" ).dialog( "open" );
        return false;
    });
});
</script>

使用以下命令启动它:

<a href="#" class="buttonintable selectFolderOpen">Select Folder</a>

1
你选择器中的 a.selectFolderOpen 中的 a 可以删除。也许以后你想把它变成一个 span 或者其他什么东西,我倾向于只使用类名而不是过于狭隘的作用域。 :p - nzifnab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接