最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - change CSS of span inside a clicked div - Stack Overflow

programmeradmin5浏览0评论

I have a table:

<table>
  <tr class="row">
    <td>
        Click me! 
        <span class="expand" style="display: none;">Hidden text</span>
    </td>
  </tr>

  <tr class="row">
    <td>
        Click me! 
        <span class="expand" style="display: none;">Hidden text</span>
    </td>
  </tr>
</table>

The idea being that each row can be clicked, and clicking the row will reveal the hidden span in the cell. Here's the jQuery I've been trying:

$("#row").click(function () {
    $(this).class(".expand").style = "";
});

I am using $(this) because there are many spans of the same class and I only want to expand the span within the clicked row.

I have a table:

<table>
  <tr class="row">
    <td>
        Click me! 
        <span class="expand" style="display: none;">Hidden text</span>
    </td>
  </tr>

  <tr class="row">
    <td>
        Click me! 
        <span class="expand" style="display: none;">Hidden text</span>
    </td>
  </tr>
</table>

The idea being that each row can be clicked, and clicking the row will reveal the hidden span in the cell. Here's the jQuery I've been trying:

$("#row").click(function () {
    $(this).class(".expand").style = "";
});

I am using $(this) because there are many spans of the same class and I only want to expand the span within the clicked row.

Share Improve this question asked Jan 22, 2014 at 16:16 John 'Mark' SmithJohn 'Mark' Smith 2,63410 gold badges47 silver badges70 bronze badges 2
  • api.jquery./category/traversing – Jason P Commented Jan 22, 2014 at 16:18
  • Use the css method in the answers below. Also, isn't a class supposed to be ".row" instead of "#row"? – Uxonith Commented Jan 22, 2014 at 16:20
Add a ment  | 

4 Answers 4

Reset to default 3
$(".row").click(function () {
    $(this).find("span.expand").show(); //Shows specific span for each row
});

Note: #row is wrong since row is a class. Use .row

This should sort you out :

jQuery:

$('.row').click(function(){
    $(this).find('span').show();
});

*Note that each row has a class of 'row' so the element is '.row' not '#row' (which would refer to an ID of 'row')

Use .css() to set style via jQuery

$(".row").click(function () {
    $(this).find(".expand").css('color','red');
    //to show use
    $(this).find(".expand").show();
});

.find()

Also read

css with jQuery

Use this code...

$('.row').click(function(){      // row is class not id
     $(this).find('span').show();
});

If you want to toggle the display (show when hidden and hide when shown) then use..

$('.row').click(function(){      // row is class not id
     $(this).find('span').toggle();
});
发布评论

评论列表(0)

  1. 暂无评论