zoukankan      html  css  js  c++  java
  • 如何阻止a链接的默认行为并获得href属性

    直接在绑定事件后添加return false 或 

    this.blur();window.event.returnValue = false;
    html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="meishu.js" type="text/javascript"></script>
    </head>
    <body>
        <ul id="list">
            <li>
                <a href="img/04.png" title="hahhah">第一张</a>
            </li>
        </ul>
    
        <img id="placeholder" src="img/06.png" alt="">
        <p id="description">内容</p>
    </body>
    </html>
    js
    window.onload=function(){
        function show(whichpic){
            var source=whichpic.getAttribute("href");
            var placeholder=document.getElementById("placeholder");
            placeholder.setAttribute("src",source);
            var text=whichpic.getAttribute("title");
            var description=document.getElementById("description");
            description.firstChild.nodeValue=text;
           
    }
    
        var list=document.getElementById("list");
        var links=list.getElementsByTagName("a");
        for(var i=0;i<links.length;i++){
            links[i].onclick=function(){
                show(this);
                return false;
            // this.blur(); 
            // window.event.returnValue = false;
            }
        }
    };
     
  • 相关阅读:
    2016/3/28 大三下第一月
    hihoCoder1284机会渺茫(唯一分解定理 + 约分)
    瞬沐队小组会议
    本周编程
    团队简介及团队课题
    本周编程
    Python环境的配置
    疫情图表
    本周编程
    返回一个数组中和最大的子数组
  • 原文地址:https://www.cnblogs.com/feilu2016/p/7159903.html
Copyright © 2011-2022 走看看