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

javascript - How can I append some html inside <td> using jquery - Stack Overflow

programmeradmin1浏览0评论

I'm trying to add a <hr> element into a <td> in jquery based on the class. I've managed to find the <td> elements I want to append without a problem, but I can't add the html - the <td> already contains some html, I want to append this on the end.

Here is what I'm trying:

<script>
$( document ).ready(function() {
    var something = "something";
    var element = $('td').filter(':contains(something)').html('<p>Hello</p>');
            console.log(element);
});
</script>

Thanks

I'm trying to add a <hr> element into a <td> in jquery based on the class. I've managed to find the <td> elements I want to append without a problem, but I can't add the html - the <td> already contains some html, I want to append this on the end.

Here is what I'm trying:

<script>
$( document ).ready(function() {
    var something = "something";
    var element = $('td').filter(':contains(something)').html('<p>Hello</p>');
            console.log(element);
});
</script>

Thanks

Share Improve this question edited Feb 21, 2017 at 12:25 Mayank Patel 1,5711 gold badge14 silver badges20 bronze badges asked Feb 21, 2017 at 12:15 Ruth YoungRuth Young 8882 gold badges15 silver badges38 bronze badges 4
  • 1 Can you please share markup as well? – Rajesh Commented Feb 21, 2017 at 12:16
  • 6 Then use .append() instead of .html() – Andreas Commented Feb 21, 2017 at 12:17
  • 1 filter(':contains(something)') would be filter(':contains('+something+')') – Roy Bogado Commented Feb 21, 2017 at 12:18
  • My filter wasn't working properly @Roy this was the fix thanks – Ruth Young Commented Feb 21, 2017 at 12:24
Add a ment  | 

3 Answers 3

Reset to default 4

Instead of using .html() you can use .append() which just added whatever you write into the end of the div.

Like this

$('td').filter(':contains(something)').append('<p>Hello</p>')

This will put hello after filtered <td>

You can also use .prepend() to add things to the begining of the div.

Take a look to this: https://jsfiddle/jo2t5yL0/1/
It's working with <td> and .html() Maybe you forget to include jquery lib?

try appending..something like this... and make sure you're using the jquery library

var element =$('td').filter(':contains(something)').append('<p>Hello</p>');
发布评论

评论列表(0)

  1. 暂无评论