简体   繁体   English

如何使用Jquery通过它的属性值显示DIV

[英]How can I show a DIV by it's attribute value using Jquery

I am working with a Jquery plugin and I would like to trigger the modal (div) by calling it's value instead of calling it's ID name. 我正在使用Jquery插件,我想通过调用它的值而不是调用它的ID名来触发模态(div)。 So if the attribute value is "554" meaning attrId="554" I will display the modal with the matching "554" attribute. 因此,如果属性值为“554”,意味着attrId =“554”,我将显示具有匹配“554”属性的模态。 Please keep in mind that the attribute value could be a variable. 请记住,属性值可以是变量。

My JSFiddle Code Example is here 我的JSFiddle代码示例在这里

;(function($) {

     // DOM Ready
    $(function() {

        // Binding a click event
        // From jQuery v.1.7.0 use .on() instead of .bind()
        $('#my-button').bind('click', function(e) {

            // Prevents the default action to be triggered. 
            e.preventDefault();

            // Triggering bPopup when click event is fired
            $('#element_to_pop_up').bPopup();

        });

    });

})(jQuery);

Any help is appreciated. 任何帮助表示赞赏。 Thanks so much 非常感谢

You can use an attribute equals selector : [attribute="value"] 您可以使用等于选择器属性[attribute="value"]

If your popup div has an attribute like this: 如果你的popup div有这样的属性:

<div id="element_to_pop_up" attrId="554">
    <a class="b-close">x</a>
    Content of popup
</div>

You can use the following: 您可以使用以下内容:

var x = '554';
$('div[attrId="' + x + '"]').bPopup();

jsfiddle 的jsfiddle

Ultimately it needs a unique selector unless you are okay with triggering multiple modals. 最终它需要一个独特的选择器,除非您可以触发多个模态。 One way to do it is to use the jQuery each function, and check each div for the matching attribute. 一种方法是使用jQuery的每个函数,并检查每个div的匹配属性。

$( "div" ).each(function() {
    var criteria = 'example_criteria';
    if ($( this ).attr( "attributename" ) == criteria)
    {
         $(this).bPopup();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM