2011-08-15 8 views
2

私はシンプルなJQueryソリューションを使用してリンクからhrefを取得し、それをクリックイベントに適用します。これはWebkitとGeckoのブラウザでは機能しますが、Internet Explorer(& 8)はhrefの位置をと定義していませんと表示し続けます。誰でもこれを修正しましたか?これを解決するのに役立つことができますか?もしそうなら大いに感謝します。.attr( 'href')returns 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> 
+6

? [テストケースを作ってください](http://jsfiddle.net)。 –

+0

こんにちは@jhuiあなたのテーブルtrのHTMLの例を投稿することができますか? ta。 –

+0

テーブル行のクラスが別のelemntのIDでない限り、実際に何も選択していません。 を含むテーブルのhtmlを投稿できますか? – yoozer8

答えて

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(); 
}); 

私はデモ here を行なったし、それは、このことができますIE 7,8および9希望に動作します。

+0

問題は、テーブル行のリンクからhrefを取得できないという問題です。それは、TRのクラスと同じIDを持つdivからhrefを取得しています。 – jhui

+0

var hrefLocation = $( '#' +要素+ '.deal-holder a')。attr( 'href'); – jhui

+0

ああ、私は参照してください。更新された答え。 – Tomgrohl

1

あなたのHTMLコードの残りの部分がわからない場合は、あなたの問題が何であるかを正確に知るのは難しいですが、HTMLの構造に大きく依存します... Example、ここでは簡単なテストをします:$(this)子供のリンクを取得する。

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; 
    } 
); 
0

準備完了機能でラッピングしてください。それはだ前に
IEはDOMで、オブジェクトにイベントを追加しようとすることができます

あなたの全体のHTML文書の
$(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; 
     }); 

}); 
関連する問題