【问题标题】:jQuery issue. Button clicked when trying to use remove()jQuery 问题。尝试使用 remove() 时单击按钮
【发布时间】:2017-01-14 06:29:22
【问题描述】:

按下“removeclass”按钮时出现问题。删除按钮应该删除一个按钮。它会这样做,但按钮也会被按下,这是不应该发生的,并且会改变视频播放器的时间。

如何在删除之前禁用该按钮?我试过使用 .prop("disabled", true) 没有运气。

 //remove button function
$("body").on("click",".removeclass", function(e){ 
    var site = document.URL;
    if (site.includes('#')) {
        site = site.substring(0, site.length - 1);
    }
    $(this).parent('div').children().prop("disabled", true);
    timearray = JSON.parse(localStorage.getItem(site));
    timepoint = parseInt($(this).parent('div').attr("id"), 10);
    var index = timearray.indexOf(timepoint);
    if (index > -1) {
        timearray.splice(index, 1);
        localStorage.setItem(site, JSON.stringify(timearray));
        console.log(JSON.parse(localStorage.getItem(site)));
        $(this).parent('div').remove();
    }
});

//Creates button
function createButton(timepoint) {
    var dispTime = convertTime(parseInt(timepoint, 10));
    var r = $('<div id='+ timepoint + ' style="float: left"> <input type="button"  ID='+ timepoint + ' value=' + dispTime + '> <a href="#" class="removeclass">Remove</a> </div>');
    timepoint = parseInt(timepoint, 10);
    r.click(function() {
        $('video').get(0).currentTime = timepoint;
    });

    $("#watch-headline-title").append(r);
}

【问题讨论】:

    标签: javascript jquery google-chrome-extension youtube-api


    【解决方案1】:
    //You should to use e.preventDefault(); at first line of function asfollowing.
    
    //Please try and let me know if not fixed.  
    
    $("body").on("click",".removeclass", function(e){ 
        e.preventDefault(); // Please add this line here
        var site = document.URL;
        if (site.includes('#')) {
            site = site.substring(0, site.length - 1);
        }
        $(this).parent('div').children().prop("disabled", true);
        timearray = JSON.parse(localStorage.getItem(site));
        timepoint = parseInt($(this).parent('div').attr("id"), 10);
        var index = timearray.indexOf(timepoint);
        if (index > -1) {
            timearray.splice(index, 1);
            localStorage.setItem(site, JSON.stringify(timearray));
            console.log(JSON.parse(localStorage.getItem(site)));
            $(this).parent('div').remove();
        }
    });
    

    【讨论】:

    • 您好,感谢您的回复。不幸的是 e.preventDefault();没有修复错误,按钮仍然被点击。该脚本也位于扩展程序的内容脚本中。
    • @pcpetepete 和 Obaidul Kader,这行不通。当.removeclass 事件处理程序被调用时,另一个处理程序已经被执行。 preventDefault() 无法阻止已经发生的事情。此外,即使此处理程序在另一个处理程序之前被调用,另一个处理程序也不是默认操作。因此,preventDefault() 在阻止它被调用方面仍然无效。
    【解决方案2】:

    精简原始代码以演示问题:
    “按钮”操作处理程序(放置在&lt;div&gt; 上)在.removeclass 处理程序之前调用:

    //Removed lines not needed to demo problem
    //Changed HTML to give unique IDs to the <div> and <input>
    
    //remove button function
    $("body").on("click",".removeclass", function(e){ 
        console.log('In .removeclass click handler.');
        $(this).parent('div').children().prop("disabled", true);
        console.log('Removing');
        $(this).parent('div').remove();
    });
    
    //Creates button
    function createButton(timepoint) {
        var dispTime = timepoint; //changed for testing convertTime() not provided
        var r = $('<div id='+ timepoint + ' style="float: left"> <input type="button"  ID='+ timepoint + ' value=' + dispTime + '> <a href="#" class="removeclass">Remove</a> </div>');
        r.click(function(e) {
            console.log('In <div> "button" click handler.');
        });
        $("#watch-headline-title").append(r);
    }
    createButton('the-Div-button');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="watch-headline-title"></div>

    对此最简单的解决方案是将“按钮”事件处理程序放在&lt;input&gt; 而不是&lt;div&gt;

    你可以通过换行来做到这一点:

    r.click(function() {
    

    r.find('input').click(function() {
    

    当你这样做时,只有.removeclass 处理程序会在你点击删除时被调用:

    //Removed lines not needed to demo problem
    //Changed HTML to give unique IDs to the <div> and <input>
    
    //remove button function
    $("body").on("click",".removeclass", function(e){ 
        console.log('In .removeclass click handler.');
        $(this).parent('div').children().prop("disabled", true);
        console.log('Removing');
        $(this).parent('div').remove();
    });
    
    //Creates button
    function createButton(timepoint) {
        var dispTime = timepoint; //changed for testing convertTime() not provided
        var r = $('<div id='+ timepoint + ' style="float: left"> <input type="button"  ID='+ timepoint + ' value=' + dispTime + '> <a href="#" class="removeclass">Remove</a> </div>');
        r.find('input').click(function(e) {
            console.log('In <input> "button" click handler.');
        });
        $("#watch-headline-title").append(r);
    }
    createButton('the-Div-button');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="watch-headline-title"></div>

    注意:您分配给 &lt;div&gt;&lt;input&gt; 的 ID 是相同的。这不应该是这样。此外,您不要在 HTML 中为这些 ID 加上双引号。因此,根据传递给createButton 的内容,HTML 可能无效。

    【讨论】:

    • 您好! @梅肯。这完美地解决了它!感谢您的帮助。
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多