【问题标题】:Go to image link in iframe after alert警报后转到 iframe 中的图像链接
【发布时间】:2016-12-10 13:52:22
【问题描述】:

我想点击iframe中的图片链接并提示ok后,它会转到iframe中的图片链接(即图片链接在新页面中打开。)我该怎么办?

DEMO

$("body").on("click", ".mylink", function(event) {
        alert('ok'); //should alert ok
    });
.addv {
	position:relative;
	float:left;
}
.mylink {
	position:absolute;
	top:0;
	left:0;
	width:100%;
	height:100%;
	z-index:2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="addv"> <a class="mylink"></a> 
		<script type="text/javascript">var anetwork_pram = anetwork_pram || [];anetwork_pram["aduser"] = "1423687058";anetwork_pram["adheight"] = "250";anetwork_pram["adwidth"] = "300";</script><script type="text/javascript" src="http://static-cdn.anetwork.ir/showad/pub.js"></script> 
</div>

【问题讨论】:

    标签: javascript jquery html css iframe


    【解决方案1】:

    为了防止事件重定向你,你只需要在你的javascript中添加event.preventDefault(),就像这样:

    $("body").on("click", ".mylink", function(event) {
        event.preventDefault();
        alert('ok'); //should alert ok
    });
    

    点击应该只会触发警报

    我什至会补充,因为您使用的是 jQuery,您可以将整个替换为:

    $(".mylink").click(function(event) {
       event.preventDefault();
       alert('ok');
    });
    

    编辑:我不明白您要打开哪个链接,但是为了在警报完成后添加重定向,只需手动进行:

       $(".mylink").click(function(event) {
          event.preventDefault();
          alert('ok');
          var url = "http://www.google.com/"; //Define your url here
          window.location = url;
       });
    

    编辑编辑:如果您的代码是

    $(function(){ $(".addv iframe").load(function(){ var iBody = $(".addv iframe").contents().find("body"); var iDiv = iBody.find("a"); alert(iDiv.html()) /*code to change the div content*/ });
    });

    如果你正确获得了 iBody 和 iDiv,那么 iDiv 就是你的 iframe 中所有可用的灯,所以你需要添加:

        var a = iDiv[0]
        $(a).click(function(event) {
            event.preventDefault();
            alert("ok");
            window.location = a.href;
        }
    

    【讨论】:

    • 好的,警报后我想转到图片链接(它在 iframe 中)。怎么样?
    • 我试过了,看到它:jsfiddle.net/jwwppgvr/1 用于获取图像链接并重定向到它。但不要为我工作。 ?
    • 我还是不明白你要做什么,你的 .addv div 中没有 iframe...
    • 通常情况下,当您单击图像时会打开一个新页面。现在我想在打开新标签之前发出警报。(我想在新页面中打开会显示警报。)如何可以吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 2017-03-16
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    相关资源
    最近更新 更多