检测_self或_blank,解析href中的URL到window.open(URL,'_self')或window.open(URL,'_blank') [英] Detect either _self or _blank, parse URL in href to function and window.open(URL, '_self') or window.open(URL, '_blank')
问题描述
在插入某些jQuery DIV
s滚动器后,我花了过去2天修复了故障 href =
滚动条。
我在这些DIV中有 href =
链接,点击它们但浏览器没有做任何事情,无论他们是 _self
或 _blank
。
,通过 window.open()
进行破解,从而产生 window.open(URL,'_self')
或 window.open(URL,'_blank')
。
我想创建一个jQuery函数,通过 .class
作为选择器,检测原始 href =
是否为目标 _self
或 _blank
,然后解析href中的URL到相应的if else中并使链接正常工作。
清除事情:
常规< a href =http://www.google.comtarget =_ blank>或< a href =http://www.yahoo.comtarget =_ self>在应用上述jQuery内容滚动器后不起作用。
因此,我使用< a onclick =window.open('http:// www。然而,我试图通过一个jQuery函数来做到这一点,而不是每次都内联。这个函数能够检测目标是_blank还是_self,捕获URL并分别使用window.open()分别返回_blank或_self。
你说什么很奇怪。我不知道为什么你的链接不能在浏览器中工作,但这是你想要的:
$(document).ready (function(){
$('。links')。click(function(){
var href = $(this).attr(href);
var target = $ (this).attr(target);
window.open(href,target);
});
});
I had spent the past 2 days fixing malfunction href=""
links after inserting certain jQuery DIV
s scroller with scrollbar.
I have href=""
links in these DIVs, clicked on them but browser not doing anything, whether they are _self
or _blank
.
Hence, the hack via window.open()
, resulting either window.open(URL, '_self')
or window.open(URL, '_blank')
.
I would like to create a jQuery function, via .class
as selector, detect whether the original href=""
is of target _self
or _blank
, and parse the URL from href to respective if else in the function and make the links work.
to clear things up:
conventional <a href="http://www.google.com" target="_blank"> or <a href="http://www.yahoo.com" target="_self"> doesn't work after applying above said jQuery content scroller.
Hence, I worked around using <a onclick="window.open('http://www.google.com/', '_blank');">
However, I was trying to do this via a jQuery function instead doing inline everytime. This function is capable to detect whether target is _blank or _self, capture the URL and return back using window.open() with _blank or _self respectively.
What did you say is very strange. I don't know why your links are not working in browser but here is what you want:
$(document).ready(function(){
$('.links').click(function(){
var href = $(this).attr("href");
var target = $(this).attr("target");
window.open(href, target);
});
});
这篇关于检测_self或_blank,解析href中的URL到window.open(URL,'_self')或window.open(URL,'_blank')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!