Using variables to implement dynamic selection examples in the jquery selector _jquery

Source: Internet
Author: User

Example one:

<table>
  <tr>
    <th> user name </th>
    <th> status </th>
  <tr>
  < Tr>
    <td> John </td>
    <td data-uid= "10000" > Normal </td>
  <tr>
  <tr>
    <td> Dick </td>
    <td data-uid= "10001" > Frozen </td>
  <tr>
  <tr>
    <td> Wang Leper </td>
    <td data-uid=10002> freeze </td>
  <tr>
</table>

<script type= "Text/javascript" >
$ (document). Ready (function () {
  var uid = 1001;
  $ ("Td[data-uid =" + UID + "]"). html (' normal ');
</script>

Example two:

<script type= "Text/javascript" >
 $ (function () {
  alert (123);
  var v=4;
  var test=$ ("Input[type= ' Radio '][value= '" +v+ "]")//direct concatenation of strings can be
  console.info (test);
  var testvalue=test.attr ({"Checked": true});
  Console.info (TestValue);
 }); 
 </script> <body> This are my
  JSP page. <br>
  <table>
 <tr>
 < td> sex:</td>
 <td>
  <input name= "Sex" type= "Radio" value= "0"/> Men 0
  <input name= " Sex "type=" Radio "value=" 1 "/> Women 1 <input name=" Sex "
  type=" Radio "value=" 2 "/> Women 2
  <input name=" Sex " Type= "Radio" value= "3"/> Female 3
  <input name= "Sex" type= "Radio" value= "4"/> Women 4
 </td>
 </ tr>
  </table>
 </body>

Example three, the problem that the selector parameter uses the variable in jquery should notice

This is the original code.

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 ("Home", $newavalue);

var $afterresource = $resource. Replace ($newavalue, "");

var $afterresource = $newavalue + $afterresource. Replace ("Home", $newavalue);

Implementation is the keyword replacement, but to the third line when not executed, debugging Ah, replace Ah, no. From the morning to just have been in various groups of questions, finally ... The Lomu of our base camp saw the blood of the Great God:

You're not writing it right.

To connect a character

$ ("Div.content:nth-child ($content _index)");

To

$ ("Div.content:nth-child (" + $content _index + ")");

The point is, there's quotes outside.

There are quotes being processed as strings

Really now feel, some basic things go wrong, light on their own debugging can not find the problem. For example, the + number just now, I have not seen the book. Baidu also does not know what the keyword. I really do not know the   selector with a variable to use the + number, that "sharp jquery" also does not explicitly say the selector inside with a variable to use the + number, including our w3cschool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.