zoukankan      html  css  js  c++  java
  • <a href="onclick="javascript:goSearch(this)" class="click" name="Java">Java</a>为什么a标签的父节点获取不到

    <script>

    function goSearch(event) {
    //var select = $('#keyInput').val($(event).attr("name"));
    var hotkey = $(event).attr("name");
    var inputkey=$('#keyInput').val();
    var searchUrl = "http://localhost:8959/CampusPostSearch.aspx?t=";
    searchUrl = searchUrl + "&zw=";
    searchUrl = searchUrl + "&cs=";
    searchUrl = searchUrl + "&we=1001"; //bys站点只搜索工作经验为'应届生'的职位(1001)
    if (hotkey == undefined) var searchKey = inputkey;
    else var searchKey = hotkey;
    //if (searchKey == "请输入职位或公司关键字") searchKey = "";
    searchUrl = searchUrl + "&qt=" + encodeURI(searchKey);
    window.location.href = searchUrl;
    //window.open(searchUrl);
    }

      }

    </script>
    <div name="d1"><a id="a1" href="javascript:goSearch(this)"> 点 DIV 1 出错</a></div>
    <div name="d2"><a id="a2" href="#" onclick="javascript:goSearch(this)">点 DIV 2 正常</a></div>
    <div name="d3"><a id="a3" href="javascript:goSearch(document.getElementById('a3'))">点 DIV 3 正常</a></div>

    感觉主要的问题不在于标签,而在于调用函数时传入的 this。
    1. this用于href中不正常
    2. this用于事件中如onclick中正常。
    3. href中如果不用this,用document.getElementById("a3")传入一个确定对象,正常。

    原因粗解,第一个取到的window对象,第二三取到的才是a标签的对象
  • 相关阅读:
    这是我
    团队项目:Recycle
    四则运算生成器
    vim记录
    常用逻辑门及其符号
    shell记录
    用Gvim建立IDE编程环境 (Windows篇)
    vim基本操作
    vim后台运行程序
    快速提高 Vi/Vim 使用效率的原则与途径
  • 原文地址:https://www.cnblogs.com/zhanghongqiang/p/3977050.html
Copyright © 2011-2022 走看看