例子一:<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。