2

我正在使用一个简单的 JQuery 解决方案从链接中获取 href 并将其应用于单击事件。这适用于 Webkit 和 Gecko 浏览器,但是 Internet Explorer (7 & 8) 一直将 href 位置显示为undefined。有人对此有修复吗?可以帮我解决这个问题吗?如果是这样,非常感谢。

 $('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>
4

4 回答 4

4

没有什么让我觉得错了。

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

我更改了您的代码,因此其中的链接tr具有点击事件:

$('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。希望这会有所帮助。

于 2011-08-15T15:31:30.030 回答
1

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

于 2011-08-15T15:40:59.813 回答
0

尝试将其包装在 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; 
      });

});
于 2011-08-16T17:21:10.140 回答
0

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

$('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; 
    }
);
于 2011-08-15T15:44:43.123 回答