Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用.data()方法将开关传递给函数_Jquery_Function - Fatal编程技术网

Jquery 使用.data()方法将开关传递给函数

Jquery 使用.data()方法将开关传递给函数,jquery,function,Jquery,Function,我在单击div.artLink时调用了一个模态对话框,请参见下文。但是,我想在访问者单击锚元素时抑制模态对话 我曾尝试使用.data方法将锚点被单击的事实传递给我的模态函数,但没有成功。 我对jQuery的了解有限,所以如果这是一个幼稚的问题,我深表歉意 有人能把我推向正确的方向吗 <div class="artLink"> <img src="/assets/press/article03_thumb.jpg" alt="" /> <div cla

我在单击div.artLink时调用了一个模态对话框,请参见下文。但是,我想在访问者单击锚元素时抑制模态对话

我曾尝试使用.data方法将锚点被单击的事实传递给我的模态函数,但没有成功。 我对jQuery的了解有限,所以如果这是一个幼稚的问题,我深表歉意

有人能把我推向正确的方向吗

<div class="artLink">
    <img src="/assets/press/article03_thumb.jpg" alt="" />
    <div class="article">
        <img src="/assets/press/article03_main.jpg" alt="" />
    </div>
    <ul class="paLinks">
        <li>MODAL DIALOGUE</li>
        <li><a href="<url>" >EXTERNAL LINK</a></li>
    </ul>
</div>
使用event.stopPropagation。它防止事件从dom树冒泡,并使用event.preventDefault停止标记中href的默认操作

// Detect if an anchor has been clicked...
$('ul.paLinks a').on('click', function(event) {
    event.stopPropagation();
    event.preventDefault();
    $(this).data('clicked', true);
});

// Load modal dialog on click
$('.artLink').click(function () {                             

    // Only activate modal dialogue if an anchor has NOT been clicked...
    if (!$(this).data('clicked')) {
        $('.article',this).modal(... );
    }
});

您可以使用更简单的检查。只要确保e.target.nodeName!='A’


但为什么是事件。错误;?OP不想禁用锚元素的行为。您正在将数据附加到锚元素本身,但随后检查父artLink元素的数据对象。事件冒泡是个问题谢谢您,Yury-这正是我想要的。
// Detect if an anchor has been clicked...
$('ul.paLinks a').on('click', function(event) {
    event.stopPropagation();
    event.preventDefault();
    $(this).data('clicked', true);
});

// Load modal dialog on click
$('.artLink').click(function () {                             

    // Only activate modal dialogue if an anchor has NOT been clicked...
    if (!$(this).data('clicked')) {
        $('.article',this).modal(... );
    }
});
$('.artLink').click(function(e){

   if(e.target.nodeName !== 'A') {// or use jQuery.fn.is for complex criteria
        console.log(this); //invoke modal logic here
   }
});