admin管理员组文章数量:1022964
I have to change a class name in my img tag
<img src="img/nex2.jpeg" class="name">
Now on a hover function in jquery I want to add a class name to it. So after hovering this tag should look like
<img src="img/nex2.jpeg" class="name secondname">
And once again on removal it should change to
<img src="img/nex2.jpeg" class="name">
I have to change a class name in my img tag
<img src="img/nex2.jpeg" class="name">
Now on a hover function in jquery I want to add a class name to it. So after hovering this tag should look like
<img src="img/nex2.jpeg" class="name secondname">
And once again on removal it should change to
<img src="img/nex2.jpeg" class="name">
Share
Improve this question
asked Mar 26, 2015 at 17:11
Yogesh GhaturleYogesh Ghaturle
2673 silver badges17 bronze badges
2
- 3 Here are the docs and examples api.jquery./hover – Huangism Commented Mar 26, 2015 at 17:13
- 2 It seriously looks like you might want to use css here if you want specific styling for hovered elements – KJ Price Commented Mar 26, 2015 at 17:13
3 Answers
Reset to default 8Here is what you need:
jQuery(function($) {
$('img.name').hover(function() {
$(this).addClass('secondname');
},
function() {
$(this).removeClass('secondname');
});
});
Or better to use CSS:
img.name:hover {
// do your styling here
}
$("img").hover(
function() {
$( this ).toggleClass('second');
}, function() {
$( this ).toggleClass('second');
}
);
.second {
border: 2px solid red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://lorempicsum./futurama/200/200/1" class="name">
Answer:
The Complete Code in JavaScript goes like this:
This gives you plete freedom to do any styling using JavaScript and is faster than jQuery.
<script type="text/javascript">
function addClass()
{
document.getElementById("apple").className += " secondname";
}
function removeClass()
{
//replaces all classes with initial class
document.getElementById("apple").className = "name";
}
window.onload = function()
{
document.getElementById("apple").addEventListener( 'mouseover' , addClass );
document.getElementById("apple").addEventListener( 'mouseout' , removeClass );
}
</script>
...
<img src="img/nex2.jpeg" id="apple" class="name">
Remember that using CSS is fastest:
img.name:hover
{
//paste all styles of class name secondname
}
This way you don't have to create two classes. Only name class is enough.
Rest goes with your styling, it's up to you how you style.
I have to change a class name in my img tag
<img src="img/nex2.jpeg" class="name">
Now on a hover function in jquery I want to add a class name to it. So after hovering this tag should look like
<img src="img/nex2.jpeg" class="name secondname">
And once again on removal it should change to
<img src="img/nex2.jpeg" class="name">
I have to change a class name in my img tag
<img src="img/nex2.jpeg" class="name">
Now on a hover function in jquery I want to add a class name to it. So after hovering this tag should look like
<img src="img/nex2.jpeg" class="name secondname">
And once again on removal it should change to
<img src="img/nex2.jpeg" class="name">
Share
Improve this question
asked Mar 26, 2015 at 17:11
Yogesh GhaturleYogesh Ghaturle
2673 silver badges17 bronze badges
2
- 3 Here are the docs and examples api.jquery./hover – Huangism Commented Mar 26, 2015 at 17:13
- 2 It seriously looks like you might want to use css here if you want specific styling for hovered elements – KJ Price Commented Mar 26, 2015 at 17:13
3 Answers
Reset to default 8Here is what you need:
jQuery(function($) {
$('img.name').hover(function() {
$(this).addClass('secondname');
},
function() {
$(this).removeClass('secondname');
});
});
Or better to use CSS:
img.name:hover {
// do your styling here
}
$("img").hover(
function() {
$( this ).toggleClass('second');
}, function() {
$( this ).toggleClass('second');
}
);
.second {
border: 2px solid red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://lorempicsum./futurama/200/200/1" class="name">
Answer:
The Complete Code in JavaScript goes like this:
This gives you plete freedom to do any styling using JavaScript and is faster than jQuery.
<script type="text/javascript">
function addClass()
{
document.getElementById("apple").className += " secondname";
}
function removeClass()
{
//replaces all classes with initial class
document.getElementById("apple").className = "name";
}
window.onload = function()
{
document.getElementById("apple").addEventListener( 'mouseover' , addClass );
document.getElementById("apple").addEventListener( 'mouseout' , removeClass );
}
</script>
...
<img src="img/nex2.jpeg" id="apple" class="name">
Remember that using CSS is fastest:
img.name:hover
{
//paste all styles of class name secondname
}
This way you don't have to create two classes. Only name class is enough.
Rest goes with your styling, it's up to you how you style.
本文标签: javascriptChange class name of a img tag in jqueryStack Overflow
版权声明:本文标题:javascript - Change class name of a img tag in jquery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745554838a2155812.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论