我有一个带iframe窗口的html页面。我想要一个变量,当iframe的URL与iframe中的导航时,iframe的URL保持与初始相同并且变量为1时,将值捕获为0。
答案 0 :(得分:1)
也许是这样的:
var clicked = 0;
$('iframe').load(function(){
var iframe = $(this).contents();
//notices if a tag is clicked within iframe
iframe.find("a").click(function(){
clicked = 1;
});
});
或者你可能意味着这样的事情(?):
var clicked = 0;
$('iframe').load(function(){
//store URL
var src = $("a").attr('src');
var iframe = $(this).contents();
//notices if a tag is clicked within iframe
iframe.find("a").click(function(){
//has the URL changed?
if(src != $(this).attr('src')){
clicked = 1;
}else{
clicked = 0;
}
});
});
最终测试的脚本.link
是iframe中的链接:
var clicked = 0;
$(document).ready(function() {
$('iframe').load(function(){
var iframe = $(this).contents();
var src = iframe.find(".link").attr('href');
//notices if a tag is clicked within iframe
iframe.find(".link").click(function(){
//has the URL changed?
if(src != iframe.find(".link").attr('href')){
clicked = 1;
}else{
clicked = 0;
}
console.log(clicked);
});
});
});