【问题标题】:How to append css Class name to particular image src url如何将 css 类名称附加到特定的图像 src url
【发布时间】:2013-09-07 17:23:17
【问题描述】:

我在 blogspot.com 中都有某些图像,我想通过特定 div 下的 Javascript 将类名附加到它。

例如:-

<div class="outer-post">
    <img src="http://1.bp.blogspot.com/1.jpg" width="200" height="200" />
    <!-- more images -->
    <img src="http://1.bp.blogspot.com/100.jpg" width="200" height="200" />
</div>

我想全部转换为:-

<div class="outer-post">
    <img src="http://1.bp.blogspot.com/1.jpg" class="myPic" width="200" height="200" />
    <!-- more images -->
    <img src="http://1.bp.blogspot.com/100.jpg" class="myPic" width="200" height="200" />
</div>

这可能吗?

【问题讨论】:

  • 请贴出你目前尝试过的代码。

标签: javascript jquery append


【解决方案1】:

使用 jQuery $("div.outer-post img").addClass("myPic")

【讨论】:

  • 谢谢兄弟,我会将此标记为答案.. 它要求我等待 10 分钟 :)
  • 它不工作.. 外帖内有很多图像。 :(
  • 这是我的代码&lt;script type="text/javascript"&gt; jQuery(document).ready(function($) { $("div.outer-post img").addClass("myPic") }); &lt;/script&gt;
  • &lt;div class="outer-post"&gt; &lt;div class="separator" style="clear: both; text-align: center;"&gt; &lt;a href="http://4.bp.blogspot.com/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt; &lt;img border="0" height="475" src="http://4.bp.blogspot.com/1.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt; &lt;div class="separator" style="clear: both; text-align: center;"&gt; &lt;a href="http://4.bp.blogspot.com/1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt; &lt;img border="0" height="475" src="http://4.bp.blogspot.com/1.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt; &lt;/div&gt;
【解决方案2】:

您可以使用.addClass()

 $(selector).addClass(className);

根据您的要求使用

 $("div.outer-post img").addClass("myPic");

编辑:根据您的 cmets 使用

 $("div.outer-post").find("img").addClass("myPic");

JSFiddle Demo

【讨论】:

  • outer-post里面有很多图片,这段代码不起作用,这是我的代码:-&lt;script type="text/javascript"&gt; jQuery(document).ready(function($) { $("div.outer-post img").addClass("myPic") }); &lt;/script&gt;
  • 我仍然面临一些问题,但我会通过你的 jsFiddle 进行一些调试。非常感谢兄弟:)
猜你喜欢
  • 2018-09-02
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
相关资源
最近更新 更多