javascript - event.stopPropagation 不工作

标签 javascript jquery html

我正在使用视频预览系统。 我的源代码片段是

<li id="liVideoList">
    <div class="thumbitem">
        <div>
            <a>
                <img alt="" src="images/download.png" onclick="download('ID')" title="Download" />                                     
            </a>
        </div>
        <img class="imagThumb" alt="Thumbs" class="thumb" src="#Path#" />
    </div>                                           
</li>

点击 li 有一个预览视频的功能。它是动态的,没有问题。但是如果我点击 li 里面的下载按钮,li 和下载按钮的功能都可以使用,这意味着预览发生了意外变化。 为了避免这种情况,我在下载代码之后添加了以下功能

event.stopPropagation();

代码看起来像

function Download(Id) {    
    $.ajax({
        type: "POST",
        url: "MyWebMethods.asmx/Download",
        data: { "Id": Id}
    }).complete(function (data) {

    });
    event.stopPropagation();
}

但两个功能仍然有效

最佳答案

你可以这样做:

function DownloadAsset(AssetId, e) {

    if (!e) var e = window.event
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();

    // your ajax call
    $.ajax({....})
}

其中e在所有浏览器中指代该事件,您可以访问该事件。

关于javascript - event.stopPropagation 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927538/

相关文章:

javascript - 将循环结构转换为 JSON - 第三方 API

javascript - 删除某个元素除了某个子元素(或某些子元素)之外的所有子元素

javascript - Jquery 从文本字段中调出日期选择器

使用 include() 时 php 无法使用 bootstrap

javascript - PHP函数不显示 "&"之后的字符串

javascript - 单击超链接时如何使用 Google Maps API v3 缩放 map ?

javascript - jQuery:函数如何仅通过点 (.) 工作

javascript - 如何将数据库中的数据添加到php中的对象中以供加载时临时使用

jquery - 为什么我的下拉菜单在 ie 上的幻灯片后面?

html - 调整窗口大小时, slider 图像未占用全宽,因此会出现一些背景空间