Truy cap cac phan tu DOM

jQuery ho tro cac phuong thuc truy cap cac phan tu trong trang web sau: theo ID, theo ten the HTML, theo thuoc tinh HTML, theo ten CSS, theo XPath.

1. Thong qua ID
Cu phap truy cap phan tu DOM theo ID co dang: $('#ID'). Thi du 1, an phan tu DOM co ID la 'MyID':

  1. <script>
  2. <!--
  3. $(function(){
  4. $('#MyID').hide();
  5. });
  6. -->
  7. </script>

2. Thong qua ten the HTML
Cu phap truy cap phan tu DOM theo the HTML co dang: $('tagName'). Thi du 2, an tat ca cac phan tu DOM co ten the 'p':

  1. <script>
  2. <!--
  3. $(function(){
  4. $('p').hide();
  5. });
  6. -->
  7. </script>

Thi du 3, an phan tu DOM dau tien co ten the la 'DIV':

  1. <script>
  2. <!--
  3. $(function(){
  4. $('p:eq(0)').hide();
  5. });
  6. -->
  7. </script>

3. Thong qua thuoc tinh HTML
Cu phap truy cap phan tu DOM theo thuoc tinh HTML co dang $('[@key=@val]'). Thi du 4, chon lua cac phan tu DOM co ten the la 'input' va thuoc tinh 'type' la 'hidden', sau do, thay doi gia tri cua cac phan tu do thanh 'I am a hidden field':

  1. <script>
  2. <!--
  3. $(function(){
  4. $('input[@type=hidden]').each(function(){
  5. $(this).val('I am a hidden field');
  6. });
  7. });
  8. -->
  9. </script>

Ky toi:
4. Truy cap phan DOM theo phuong thuc XPath
5. Truy cap phan DOM theo phuong thuc CSS
6. Cac phuong thuc truy cap dac biet

The Hong,

Tham khao:
1. JQuery docs

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <br> <p> <h3>
  • Lines and paragraphs break automatically.
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.