setTimeout window.open 不能接受这个.href
setTimeout window.open can't take this.href
我试图在 3000 毫秒后打开 href onMouseOver。但它只是弹出一个空白窗口。我错过了什么?
.HTML:
<a href="../cc2b/myrec.html" onMouseOver="Popup = setTimeout('openwindow(this.href)',3000);" onMouseOut="clearInterval(Popup)">My Rec</a>
JavaScript:
var Popup = null;
function openwindow()
{
var win = window.open()
}
(好的,首先,你需要提供一个 URL 到 window.open()
,否则它不知道要打开到哪个页面。 除此之外:)
执行setTimeout()
时,延迟代码中的 this
值将重置。
快速解决方法是立即提取 URL,然后将函数传递到可以使用该变量的setTimeout()
中。
<a href="../cc2b/myrec.html"
onMouseOver="var popupUrl = this.href; Popup = setTimeout(function(){openwindow(popupUrl)}), 3000);"
onMouseOut="clearInterval(Popup)">
My Rec
</a>
但是,一个更简洁的解决方案是通过在openhoverpopup
函数中设置超时来最小化onMouseOver
中的代码:
<a href="../cc2b/myrec.html"
onMouseOver="openhoverpopup(this.href)"
onMouseOut="clearhoverpopup()">
My Rec
</a>
<script>
var popupTimeout = null;
function openhoverpopup(url) {
popupTimeout = setTimeout(function () {
window.open(url);
}, 3000);
}
function clearhoverpopup() {
clearTimeout(popupTimeout);
}
</script>
对于较旧的 IE 浏览器,您可以使用 event.target
或 event.srcElement
从触发鼠标悬停事件的元素中获取 URL。
http://jsfiddle.net/b42pr/1
.HTML
<a href="theURL" onmouseover="popURL()">Hover</a>
JavaScript
function popURL() {
var url = event.target.href || event.srcElement.href;
console.log("Open URL: " + url);
setTimeout(function(){
window.open(url);
}, 3000)
}
this.href
是未定义的,我认为您正在寻找window.location.href
:
> this.href
undefined
> window.location.href
"http://stackoverflow.com/questions/18981172/settimeout-window-open-cant-take-this-href"
另外,您的函数
function openwindow()
{
var win = window.open()
}
不带任何参数,也不打开任何内容。将其更改为
function openwindow(target)
{
var win = window.open(target)
}
但要小心,大多数弹出窗口阻止程序会阻止这种窗口。
尝试在字符串之外指定 href:
<a href="../cc2b/myrec.html" onMouseOver="Popup = setTimeout('openwindow(' + window.location.href + ')',3000);" onMouseOut="clearInterval(Popup)">My Rec</a>
相关文章:
- javascriptRouter 不能接受参数
- Selenium 和 Python 不能使用 .click() 点击 href=“javascript:void(0);
- 文本框接受字符和数字或字母,但不能只接受数字
- JQuery .click() 处理程序不能接受函数周围的 setTimeout()
- setTimeout window.open 不能接受这个.href
- AngularJs的ui-router ui-sref不能从嵌套状态中生成带有参数的href
- 不能使用JQuery更改href值
- href="javascript:proDoc(..不能在chrome上工作,但IE
- Angularjs的路由不能使用base href
- Javascript不能接受在视图中重写输出
- 锚href不能正确显示
- 不能在jQuery编辑中接受文本
- 不能触发点击href(强制点击href)
- JQuery .html不能在href中添加变量值
- window.location.href不能在Windows Phone 7.1/7.5上工作
- window.location.href不能在IE9下工作
- Href 不能正常工作
- 为什么不能接受拖拽的物品
- top.location.href不能在ie8中工作
- 为什么 window.location.href 不能在错误页面上工作