0

我在获取单击元素的 href 并将其传递给函数时遇到了一些问题,这是我以前没有做过的事情,所以我尽力了,但没有得到它。有任何想法吗?

$('#smoke_confirm').click(function(e){
            var href = $(this).attr("href");
            tstconfirm(href);
            e.preventDefault();
        });

        function tstconfirm(href){
            smoke.confirm('Are you sure you want to delete?',function(e){
                if (e){
                    window.location = $(href);
                }
            }, {cancel:"cancel", ok:"confirm"});
        }
4

2 回答 2

2

这里的 href 是一个文本,所以$(href)不正确,因为它会尝试选择具有 href 值的元素。做吧window.location = href。此外,如果您只想获取不需要创建 jquery 实例的 href,则this可以执行this.hrefDOM 元素属性。

$('#smoke_confirm').click(function(e){
            var href = this.href;
            tstconfirm(href);
            e.preventDefault();
        });

        function tstconfirm(href){
            smoke.confirm('Are you sure you want to delete?',function(e){
                if (e){
                    window.location = href;
                }
            }, {cancel:"cancel", ok:"confirm"});
        }
于 2013-06-17T21:16:47.277 回答
1

href是一个字符串

 window.location = $(href); // This will try to convert it to a jQuery object

应该是

 window.location = href;
于 2013-06-17T21:16:27.673 回答