如何使用Jquery显示DIV的属性值?

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

本文关键字:属性 DIV 何使用 Jquery 显示      更新时间:2023-09-26

我正在使用一个Jquery插件,我想通过调用它的值而不是调用它的ID名称来触发模态(div)。因此,如果属性值为"554"意味着attrId="554",我将显示具有匹配"554"属性的模态。请记住,属性值可以是一个变量。

我的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);

任何帮助都是感激的。Thanks so much

您可以使用属性=选择器:[attribute="value"]

如果你的弹出式div有一个这样的属性:

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

您可以使用以下命令:

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

jsfiddle

最终它需要一个唯一的选择器,除非你可以触发多个模态。一种方法是使用jQuery的each函数,并检查每个div的匹配属性。

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