例子一:
<table>
<tr>
<th>用戶名</th>
<th>狀態</th>
<tr>
<tr>
<td>張三</td>
<td data-uid="10000">正常</td>
<tr>
<tr>
<td>李四</td>
<td data-uid="10001">凍結</td>
<tr>
<tr>
<td>王二麻子</td>
<td data-uid=10002>凍結</td>
<tr>
</table>
<script type="text/javascript">
$(document).ready(function(){
var uid = 1001;
$("td[data-uid = "+ uid +"]").html('正常');
}
</script>
例子二:
<script type="text/javascript">
$(function(){
alert(123);
var v=4;
var test=$("input[type='radio'][value='"+v+"']");//直接拼接字符串就可以了
console.info(test);
var testValue=test.attr({"checked":true});
console.info(testValue);
});
</script>
<body>
This is my JSP page. <br>
<table>
<tr>
<td>性別:</td>
<td>
<input name="sex" type="radio" value="0"/>男 0
<input name="sex" type="radio" value="1"/>女 1
<input name="sex" type="radio" value="2"/>女 2
<input name="sex" type="radio" value="3"/>女 3
<input name="sex" type="radio" value="4"/>女 4
</td>
</tr>
</table>
</body>
例子三、jQuery中選擇器參數使用變量應該注意的問題
這是原來的代碼
var li_index = $(this).index(); var $content_index = li_index + 2; var $content_progress = $(“div.content:eq(” + $content_index + “)”); var $newavalue = $(this).find(“a”).attr(“name”); var $resource = $(this).find(“a”).html().replace(“首頁”,$newavalue); var $afterresource = $resource.replace($newavalue,””); var $afterresource = $newavalue + $afterresource.replace(“首頁”,$newavalue);
實現的是關鍵詞替換,不過到第三行時候不執行了,調試啊,替換啊,都不行。 從早上到剛才一直在各種群裡面發問,終於 …… 俺們大本營 的Lomu大神一陣見血:
你的寫法不對
要連接符
$(“div.content:nth-child($content_index)”);
改為
$(“div.content:nth-child(” + $content_index + “)”);
關鍵是外面有引號
有引號被當字符串處理了
說真的現在感覺,有些基礎的東西出錯,光靠自己調試根本找不出問題所在。比如剛才那個 + 號,我看書就沒見過。出現這種錯誤百度也不知道什麼關鍵詞。真不知道 選擇器 裡面用變量 還要用到+號,那個《鋒利的jQuery 》也沒有明確的說 選擇器 裡面用變量 還要用到+號,包括我們的w3cschool。