jQuery change html in $(this) object



我使用此jQuery代码来检测表中行的单击。

$('#availableApps').on('click', 'tr', function (e) {
    $(this)
});

网页标记:

<tr>
  <td><img src="http://is5.mzstatic.com/image/thumb/Purple/v4/9a/b5/39/9ab539fb-4a39-c780-e9ec-eb58f4685141/source/512x512bb.jpg" style="width:20px; height:20px; border-radius: 10px;"></td>
  <td>Lär dig läsa</td>
  <td>2<img class="pull-right" src="/Images/arrowRight.png"></td>
</tr>

现在单击我想更改上一<td>中图像的src,如何使用此$(this)对象执行此操作?

使用

find('td:last img') 获取上td中的img,然后使用如下所示attr函数更改src

$('#availableApps').on('click', 'tr', function (e) {
    $(this).find('td:last img').attr('src', 'new src');
});

尝试使用 .find() 查找img.attr()更改 src 属性,如下所示:-

$('#availableApps').on('click', 'tr', function (e) {
    $(this).find("img.pull-right").attr('src','new src');
});

或使用:last查找最后一个TD,如下所示:-

$('#availableApps').on('click', 'tr', function (e) {
    $(this).find('td:last img.pull-right').attr('src', 'new src');
});

现在单击我想更改图像的 src 最后

由于您想在单击任何列时更新最后一列的 img 源,请尝试

$(this).siblings().last().find( "img" ).attr( "src" , "new URL" );

外植

到达最后一个兄弟姐妹 - $(this).siblings().last()

进入最后一个兄弟姐妹内部的 img - $(this).siblings().last().find( "img" )

最后更新网址

使用 find on $(this).find 和 :last 获取最后一个 img,并使用 attr 设置src

$(this).find( "td:last img" ).attr( "src", "new URL" ) ;

你可以使用 jQuery .find()

$('#availableApps').on('click', 'tr', function (e) {
    $(this).last().find('img').attr("src","new src")
})
<</div> div class="one_answers">

最后td使用 :last

$('#availableApps').on('click', 'tr', function (e) {
    $(this).find('td:last img').attr('src', 'YOUR_NEW_IMAGE_SOURCE');
});

使用最后一个孩子并尝试以下代码:-

$(this).find('td:last-child').find('img').attr('src', 'new src');

$(':last-child', this).find('img').attr('src', 'new src');

$(this).find(':last-child').find('img').attr('src', 'new src');

希望它能帮助你:)

相关内容

最新更新