如何将图像插入具有特定文本的div类?

时间:2018-01-24 09:04:31

标签: jquery html css

在html中有许多具有相同名称的div类" vlt-article-content"。我试图将图像插入到课程中,文本是" Tim Pete - 搜索程序员"

这是我的jQuery:



$(".clickable-link").click(function () {
  $(this).closest('.vlt-article-content').children('h3 a').text('Tim Pete - Search for Programmers');
  $(this).prepend('<img id="theImg" class="logo" src="//i.stack.imgur.com/xY3PQ.jpg" />');
});
&#13;
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="vlt-article-content">
  <h3 class="views-field-title clickable-link">
    <a href="/article/tim-pete-search">Tim Pete - Search for Programmers</a>
  </h3>
  <div class="vlt-article-meta">
    <div class="vlt-article-date">24.01.2018 </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在jQuery中,可以通过将HTML字符串传递给构造函数来创建新元素,如下所示:

var img = $('<img id="theImg">'); //Equivalent: $(document.createElement('img'))
img.attr('src', "https://......");
img.appendTo('#vlt-article-content');