zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Jquery选择器中使用变量实现动态选择例子

jQuery变量 实现 使用 动态 选择 例子 选择器
2023-06-13 09:15:39 时间

例子一:

<table>
<tr>
<th>用户名</th>
<th>状态</th>
<tr>
<tr>
<td>张三</td>
<tddata-uid="10000">正常</td>
<tr>
<tr>
<td>李四</td>
<tddata-uid="10001">冻结</td>
<tr>
<tr>
<td>王二麻子</td>
<tddata-uid=10002>冻结</td>
<tr>
</table>

<scripttype="text/javascript">
$(document).ready(function(){
varuid=1001;
$("td[data-uid="+uid+"]").html("正常");
}
</script>

例子二:

<scripttype="text/javascript">
$(function(){
alert(123);
varv=4;
vartest=$("input[type="radio"][value=""+v+""]");//直接拼接字符串就可以了
console.info(test);
vartestValue=test.attr({"checked":true});
console.info(testValue);
});
</script>

<body>
ThisismyJSPpage.<br>
<table>
<tr>
<td>性别:</td>
<td>
<inputname="sex"type="radio"value="0"/>男0
<inputname="sex"type="radio"value="1"/>女1
<inputname="sex"type="radio"value="2"/>女2
<inputname="sex"type="radio"value="3"/>女3
<inputname="sex"type="radio"value="4"/>女4
</td>
</tr>
</table>
</body>

例子三、jQuery中选择器参数使用变量应该注意的问题

这是原来的代码

varli_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。