Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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获取<;的主机名;a>;右键单击事件上的元素_Jquery_Event Handling - Fatal编程技术网

jquery获取<;的主机名;a>;右键单击事件上的元素

jquery获取<;的主机名;a>;右键单击事件上的元素,jquery,event-handling,Jquery,Event Handling,我想在右键单击或元素时获取href的扩展名和主机名。我已经尝试了下面的代码,但是我得到了“未定义” $(document).mousedown(function(e) { if (e.which == 3) { var tag = e.target.nodeName; var href = e.target.href; if (tag == 'A') { thelink =

我想在右键单击或元素时获取href的扩展名和主机名。我已经尝试了下面的代码,但是我得到了“未定义”

$(document).mousedown(function(e) {
        if (e.which == 3) {

            var tag = e.target.nodeName;
            var href = e.target.href;

            if (tag == 'A') {
                thelink = e.target.href;
                console.log(href.hostname);

            }
            if (tag == 'IMG') {
                thelink = e.target.src;
                console.log(href.hostname);
            }
        }
    });
你可以参考:

$(文档).mousedown(函数(e){
如果(e.which==3){
var tag=e.target.nodeName;
var thelink=未定义;
如果(标记=='A'){
thelink=e.target.href;
}
如果(标记=='IMG'){
thelink=e.target.src;
}
//
//如果链接,则获取主机名。。。
//
如果(链接){
//
//将字符串转换为URL
//
var url=新url(链接);
log(url.hostname);
}
}
});

这太棒了。非常感谢。