我正在尝试制作可扩展的 h3。为此,我将有一个“加号”图像可供单击,当它被单击时必须更改为“减号”图像,我需要向 h3 添加一个 css 类以显示内容。我将粘贴下面的所有代码:
<div class="default">
[ManageBlog]
<div class="msearch-result" id="LBSearchResult[moduleid]" style="display: none">
</div>
<div class="head">
<h1 class="blogname">
[BlogName] <img src="/portals/40/Images/Train_Fitness_2015_S/images/plus_symbol.png" alt="Plus Button" id="ExpandBlogDescriptionImg"></h1> [RssFeeds]
<br style="line-height: 12px; clear: both" />
<h3 class="blogdescription">
[BlogDescription]</h3> [Author]
</div>
<br /> [Posts] [Pager]
</div>
<div style="clear: both"></div>
这是javascript:
jQuery(document).ready(function() {
$("#ExpandBlogDescriptionImg").click(function() {
var right = "https://train.fitness/portals/40/Images/Train_Fitness_2015_S/images/plus_symbol.png";
var left = "https://train.fitness/portals/40/Images/Train_Fitness_2015_S/images/minus_symbol.png";
element.src = element.bln ? right : left;
element.bln = !element.bln;
});
var img = $("#ExpandBlogDescriptionImg");
if (img.src = "https://train.fitness/portals/40/Images/Train_Fitness_2015_S/images/minus_symbol.png") {
$('.blogdescription').addClass("ExpandBlogDescriptionText");
} else {
$('.blogdescription').removeClass("ExpandBlogDescriptionText");
};
});
最佳答案
你可以使用jquery的.attr函数来设置图片源
$('.img-selector').attr('src','plusorminusimagepath.jpg');
你可以有一个 bool 标志来知道它是少了还是多了
关于javascript - 单击更改图标并通过 javascript 添加 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319344/