Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .attr('href')返回未定义的_Javascript_Jquery_Href_Window.location - Fatal编程技术网

Javascript .attr('href')返回未定义的

Javascript .attr('href')返回未定义的,javascript,jquery,href,window.location,Javascript,Jquery,Href,Window.location,我正在使用一个简单的JQuery解决方案从链接中获取href,并将其应用于单击事件。这适用于Webkit和Gecko浏览器,但是Internet Explorer 7和8会将href位置显示为未定义。有人对此有办法吗?你能帮我解决这个问题吗?非常感谢 $('table tr').click( function () { var element = $(this).attr("class"); var hrefLocation = $('#'+ eleme

我正在使用一个简单的JQuery解决方案从链接中获取href,并将其应用于单击事件。这适用于Webkit和Gecko浏览器,但是Internet Explorer 7和8会将href位置显示为未定义。有人对此有办法吗?你能帮我解决这个问题吗?非常感谢

 $('table tr').click(
    function () {
        var element = $(this).attr("class");
        var hrefLocation = $('#'+ element +' .deal-holder a').attr('href');
        alert(hrefLocation)
        window.location.href = hrefLocation;
        return false; 
    }
);
HTML很简单:

 <tr class="QRG">
    <td class="blue"><a href="#QRG">QRG</a></td>
    <td>Company Sale</td>
    <td>Technology</td>
 </tr>


 <div class="deal" id="QRG">
      <p><span class="js">Overview</span><span class="no-js">Enham</span></p>
      <div class="deal-holder">
         <div class="image-holder">
             <img src="../assets/images/enham.gif" alt="" height="70" width="150" />
         </div>
         <p>Enham is a charity established in 1918, which delivers a wide range of essential services that provide choice and empowerment to disabled people to make their own decisions about their lives. Enham is a charity established in 1918, which delivers a wide range of essential services that provide choice and empowerment to disabled people to make their own decisions about their lives</p>
         <a class="moreInfo" href="individual.html">More Information</a>
      </div>
 </div>

我错了,没什么好惹我生气的

首先确保使用最新版本的jQuery,因为这有时会有所帮助

我更改您的代码,使tr内的链接具有click事件:

$('table tr a').click(function (e) {
    var element = $(this).closest('tr').attr("class"),
        hrefLocation = $('#'+ element +' .deal-holder a').attr('href');
    alert(hrefLocation)
    window.location.href = hrefLocation;
    e.preventDefault();
});

我做了一个演示,它在IE 7、8和9中运行。希望这能有所帮助。

如果不知道html代码的其余部分,就很难确切知道问题所在,行为在很大程度上取决于html的结构。。。但是在这里做一个快速测试,它是有效的:,只需使用$this指针来获取子链接。

您可能需要选择超链接组中的第一个元素

$('table tr').click(
    function () {
        var element = $(this).attr("class");
        var hrefLocation = $('#'+ element +' .deal-holder a').eq(0).attr('href');
        alert(hrefLocation)
        window.location.href = hrefLocation;
        return false; 
    }
);

尝试将其包装到ready函数中。 IE可能试图在对象进入DOM之前将其添加到对象中:

$(document).ready(function(){

   $('table tr').click(
      function () {
         var element = $(this).attr("class");
         var hrefLocation = $('#'+ element +' .deal-holder a').attr('href');
         alert(hrefLocation);

         window.location.href = hrefLocation;
         return false; 
      });

});

这就是你的整个HTML文档。您好@jhui您能在您的表格tr中发布一个html示例吗?除非表行的类是另一个元素的id,否则实际上并没有选择任何内容。你能为包含的表发布html吗?你是在加载DOM后这样做的吗?@Jim:我怀疑类就是元素名,特别是因为他说它在非IE浏览器中工作。问题是它没有从表行中的链接获取href。它从与tr的class.var hrefLocation=$+element+'具有相同id的div获取href。交易持有者a'.attr'href';