2016-09-13 2 views
0

jquery attr(key,func(){})Jquery attr exampleattr(key,func)を使用して境界を適用しましたが、最初のテーブルタグの境界線が適用されていません。私もjQueryライブラリのdifferrentバージョンを試してみましたが、jqueryが最初のテーブルタグをレンダリングしていません

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
<html lang="en-US"> 
 

 
<head> 
 
    <title>An Example for testing | QcTutorials</title> 
 
</head> 
 

 

 
<head> 
 
    <title>The Selecter Example</title> 
 
    <script type="text/javascript" src="../../js/jquery.js"> 
 
    </script> 
 

 
    <script type="text/javascript" language="javascript"> 
 
    $(document).ready(function() { 
 

 
     $("table").attr("border", function(arr) { 
 
     return arr; 
 
     }) 
 

 
    }); 
 
    </script> 
 
</head> 
 

 
<body> 
 

 

 
    <table> 
 
    <tr> 
 
     <td>This is first table</td> 
 
    </tr> 
 
    </table> 
 

 
    <table> 
 
    <tr> 
 
     <td>This is second table</td> 
 
    </tr> 
 
    </table> 
 

 
    <table> 
 
    <tr> 
 
     <td>This is third table</td> 
 
    </tr> 
 
    </table> 
 

 
</body> 
 

 

 
</html>

+0

ボーダー属性のみ1または0を取るようにしてみてください: 'のattr( '国境'、1)' – Pete

+0

あなたが学ぶ場合、それが役立ちますいくつかの基本的なjavascriptのデバッグ手法。この場合、関数内に 'alert(または' console.log'(F11))を追加してください: '$(" table ")attr(" border "、function(arr){alert(arr);}' '$(" table ")。attr(" border "、0);'これを直接試してみてください。 –

答えて

3

まず私は、それはjqueryのlibray /ブラウザの問題だと思ったが、それは別のブラウザやjqueryのバージョンで同じ結果を与える

そのレンダリング属性は、値として表示されず、0です。 IN .attr(attributeName, function)ファンクションの最初の引数はindexなので、最初のテーブルの設定は0です。その後12 .....

$(document).ready(function() { 
 
    $("table").attr("border", function(index, value) { 
 
    return index + 1; 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td>This is first table</td> 
 
    </tr> 
 
</table> 
 

 
<table> 
 
    <tr> 
 
    <td>This is second table</td> 
 
    </tr> 
 
</table> 
 

 
<table> 
 
    <tr> 
 
    <td>This is third table</td> 
 
    </tr> 
 
</table>