锚定单击“preventDefault”,然后重新打开

发布于 2024-12-12 20:56:54 字数 1510 浏览 7 评论 0原文

下面是问题和代码:

我正在单击已经设置了 href 的锚点。

第 1 步,在锚点 fileMove 单击时,我阻止它触发原始 href,因为我正在打开一个弹出窗口,供用户单击某些内容并提供一些信息。

$('#fileMove').click(function(e) {
    alert($('#fileMove').attr('href'));
    e.preventDefault(); //prevent original href
    showdevDialog(); //dialog popup call
});

function showdevDialog() {
    $('#JQueryFTD_Demo').dialog(); //dialog with div JQueryFTD_Demo
}

// this will be in document.ready
$('#JQueryFTD_Demo').fileTree({
    root: 'D:\\Test',
    script: '../jqueryFileTree.jsp',
    expandSpeed: 1000,
    collapseSpeed: 1000,
    multiFolder: true,
    loadMessage: 'Please Wait While Loading...'
    }, function(file) {
        //alert(file);
    }, function(dir){
        moveFileToFolder(dir); //once user selects a folder passing 
                                              //it to function          
});

该函数隐藏弹出窗口,然后使用文件夹名称构造一个新的 href,然后绑定单击事件。

function moveFileToFolder(dir) {
    $('#JQueryFTD_Demo').hide();
    $('#JQueryFTD_Demo').dialog('close');
    var _href = $('#fileMove').attr("href");
    $('#fileMove').attr("href", _href + '&moveto=' + dir);
    alert($('#fileMove').attr('href'));
    $('#fileMove').unbind('click').click();
    //$('#fileMove').attr('href')
    //alert("Inside func.. " + dir);
}

但点击事件再次调用 $('#fileMove').click() ,其中包含 preventDefault 并且没有任何反应。

我该如何解决这个问题?

任何帮助表示赞赏。

Below is problem and code:

I'm clicking on an anchor which has an href set already.

Step1, upon anchor fileMove click, I'm preventing it from firing original href because I'm opening a popup for user to click something and provide some info.

$('#fileMove').click(function(e) {
    alert($('#fileMove').attr('href'));
    e.preventDefault(); //prevent original href
    showdevDialog(); //dialog popup call
});

function showdevDialog() {
    $('#JQueryFTD_Demo').dialog(); //dialog with div JQueryFTD_Demo
}

// this will be in document.ready
$('#JQueryFTD_Demo').fileTree({
    root: 'D:\\Test',
    script: '../jqueryFileTree.jsp',
    expandSpeed: 1000,
    collapseSpeed: 1000,
    multiFolder: true,
    loadMessage: 'Please Wait While Loading...'
    }, function(file) {
        //alert(file);
    }, function(dir){
        moveFileToFolder(dir); //once user selects a folder passing 
                                              //it to function          
});

This function hides the popup and then constructs a new href using the folder name then binds click event.

function moveFileToFolder(dir) {
    $('#JQueryFTD_Demo').hide();
    $('#JQueryFTD_Demo').dialog('close');
    var _href = $('#fileMove').attr("href");
    $('#fileMove').attr("href", _href + '&moveto=' + dir);
    alert($('#fileMove').attr('href'));
    $('#fileMove').unbind('click').click();
    //$('#fileMove').attr('href')
    //alert("Inside func.. " + dir);
}

But the click event once again calls $('#fileMove').click() which has preventDefault and nothing happens.

How do I get around this problem?

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-19 20:56:54

仅当对话框可见时,您可以通过执行以下操作有条件地使用 .preventDefault()

$('#fileMove').click(function(e){
    alert($('#fileMove').attr('href'));
    if ($("#JQueryFTD_Demo:visible"){
        e.preventDefault(); //prevent orignal href
        showdevDialog(); //dialog popup call
    };

});

You could use the .preventDefault() conditionally only when the dialog is visible by doing something like this:

$('#fileMove').click(function(e){
    alert($('#fileMove').attr('href'));
    if ($("#JQueryFTD_Demo:visible"){
        e.preventDefault(); //prevent orignal href
        showdevDialog(); //dialog popup call
    };

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文