使用 Jquery 交换图像
我对 Jquery 很陌生,所以请原谅这个菜鸟问题。当用户单击相应的超链接时,我试图交换
元素的背景图像。
标签是动态生成的,并且随时可以有“n”个数量,具体取决于 SQL 表中设置的图像数量。
我的 HTML 是:
<div id="feature-image">
<h1><span>A random heading</span></h1>
</div>
<div id="feature-links">
<a class="a1" href="#">1</a>
<a class="a2" href="#">2</a>
<a class="a3" href="#">3</a>
</div>
标记设置为 display:none
我编写了此 Jquery 语句,以便在用户单击 #feature-links div 中的超链接时运行。我试图将 var image
设置为 标签类的名称(a1、a2、a3)等,然后更改 CSS 背景图像属性
使用
image
var 作为 .jpg.
url 的一部分。
<script>
$(function(){
$('#feature-links a').click(function(){
var image = $(this).attr("class","");
$('#feature-image h1').css('background-image','url=(\'main_' + image + '.jpg\')');
});
});
</script>
我所有的图像都被命名为“main_a1.jpg”,“main_a2.jpg”等等...
我看不出我做错了什么,或者我试图做的事情(在变量中获取类名等)是否是实际上是否是好的做法...一些帮助将非常感激。
提前致谢!
编辑:
编辑后的 Jquery 代码:
$(function(){
$('#feature-links a').click(function(){
var image = $(this).attr('class');
$('#feature-image h1').css('background-image','url(images/main_' + image + '.jpg)');
});
});
I am very new to Jquery so please excuse this noobie question. I am trying to swap a background image of an <h1>
element when a user clicks on the corresponding hyperlink. The <a>
tags are generated dynamically and there can be 'n' quantity of them at any time depending on how many images are set in a SQL table.
My HTML is:
<div id="feature-image">
<h1><span>A random heading</span></h1>
</div>
<div id="feature-links">
<a class="a1" href="#">1</a>
<a class="a2" href="#">2</a>
<a class="a3" href="#">3</a>
</div>
The <span>
tags are set to display:none
I've written this Jquery statement to run when a user clicks on a hyperlink in the #feature-links div. I was attempting to set the var image
to the name of the <a>
tags class (a1, a2, a3) etc. and then change the CSS background image property of the <h1>
using the image
var as part of the .jpg.
url.
<script>
$(function(){
$('#feature-links a').click(function(){
var image = $(this).attr("class","");
$('#feature-image h1').css('background-image','url=(\'main_' + image + '.jpg\')');
});
});
</script>
all my images are named "main_a1.jpg", "main_a2.jpg" and so on...
I can't see what I am doing wrong or whether what I am attempting to do (grab class names etc. in vars) is actually good practice or not... some help would be really appreciated.
Thanks in advance!
EDIT:
Jquery code after edits:
$(function(){
$('#feature-links a').click(function(){
var image = $(this).attr('class');
$('#feature-image h1').css('background-image','url(images/main_' + image + '.jpg)');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将行更改为:
我相信为
.attr()
函数指定 2 个参数用于更改属性值。因此,在您的情况下,它将将该类更改为""
。指定一个参数将返回该类的值
Change line to:
I believe specifying 2 parameters to the
.attr()
function is used for changing the attribute value. Therefore in your case, it would change the class to""
.Specifying one parameter will return the value of the class
一个更好的方法是设置 href,而不是使用类。
例如:
那么您的链接将直接链接到背景图像:
希望有帮助:)
A better way, instead of using the class would be to set the href.
For example:
Then your links would simply link directly to the background image:
Hope that helps :)