admin 管理员组文章数量: 1086019
The following code adds and removes an "active" class to a menu (.active
) It also adds a span class to the active link <span class="filet_menu"></span>
.
How can I remove this span class? I've tried unwrap but it doesn't remove the span class "unwrap".
Here is my code:
$("#menu a").click(function() {
$("#menu a").each(function() {
$(this).removeClass("active");
//Don't work :
//$(this).unwrap('<span class="filet_menu"></span>');
//$(this).contents().unwrap();
//$('(this) > .active').unwrap();
});
$(this).addClass("active");
$(this).wrapInner('<span class="filet_menu"></span>');
});
.active {
color: #32c0ce !important;
}
.filet_menu {
border-bottom: 2px solid #32c0ce;
padding-bottom: 2px;
}
<script src=".3.1/jquery.min.js"></script>
<body>
<div id="header">
<div id="contenu_header">
<h1>Sébastien Gicquel</h1>
<ul id="menu">
<li><a id="bt_diaporama" href="#diaporama">Home</a></li>
<li><a id="bt_presentation" href="#presentation">Présentation</a></li>
<li><a id="bt_realisations" href="#realisations">Réalisations</a></li>
<li><a id="bt_contact" href="#contact">Contact</a></li>
</ul>
</div>
<!-- fin contenu_header -->
</div>
<!-- fin header -->
<div id="page">
The following code adds and removes an "active" class to a menu (.active
) It also adds a span class to the active link <span class="filet_menu"></span>
.
How can I remove this span class? I've tried unwrap but it doesn't remove the span class "unwrap".
Here is my code:
$("#menu a").click(function() {
$("#menu a").each(function() {
$(this).removeClass("active");
//Don't work :
//$(this).unwrap('<span class="filet_menu"></span>');
//$(this).contents().unwrap();
//$('(this) > .active').unwrap();
});
$(this).addClass("active");
$(this).wrapInner('<span class="filet_menu"></span>');
});
.active {
color: #32c0ce !important;
}
.filet_menu {
border-bottom: 2px solid #32c0ce;
padding-bottom: 2px;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div id="header">
<div id="contenu_header">
<h1>Sébastien Gicquel</h1>
<ul id="menu">
<li><a id="bt_diaporama" href="#diaporama">Home</a></li>
<li><a id="bt_presentation" href="#presentation">Présentation</a></li>
<li><a id="bt_realisations" href="#realisations">Réalisations</a></li>
<li><a id="bt_contact" href="#contact">Contact</a></li>
</ul>
</div>
<!-- fin contenu_header -->
</div>
<!-- fin header -->
<div id="page">
Share
Improve this question
edited Apr 18, 2019 at 17:16
double-beep
5,53719 gold badges40 silver badges49 bronze badges
asked Dec 7, 2012 at 15:41
Sébastien GicquelSébastien Gicquel
4,3867 gold badges57 silver badges87 bronze badges
3
- how does the html look like? – Anton Commented Dec 7, 2012 at 15:45
- Do you want to remove the element span or the class name? – Arooran Commented Dec 7, 2012 at 15:45
- I've edited the question with html code. I want to remove span class="filet_menu"></span> but i think it could work if i only remove the class filet_menu – Sébastien Gicquel Commented Dec 7, 2012 at 15:48
4 Answers
Reset to default 3You need to get to the contents first to unwrap. And you don't need the each loop
$("#menu a").click(function() {
$("#menu a.active").removeClass("active");
$(this).addClass("active");
$('span.filet_menu').contents().unwrap();// get previous span contents first and unwrap
$(this).wrapInner('<span class="filet_menu"></span>');// this wraps current anchor's contents
});
http://jsfiddle/syXnH/
Or do you really need the span? why not just add/remove the classes
$("#menu a").click(function() {
$("#menu a.active").removeClass("active filet_menu");
$(this).addClass('filet_menu active');
});
http://jsfiddle/HwTNz/
I believe you want to remove the span, but preserve the data inside the span, so the .remove() won't work for you. You can just use this:
$(this).html(
$(this).find("span.filet_menu").html()
);
Couple of suggestions for improving your code:
- Rather than adding and removing the
<span>
, create it once for each item at load time. Each time you modify the DOM the browser has to perform expensive layout calculations. You can the control it's display via CSS, depending on it's parentactive
class. - You don't need to iterate every menu item to remove the
active
class, just use a selector
JavaScript:
$("#menu a").each(function() {
$(this).wrapInner('<span class="filet_menu"></span>');
$(this).click(function() {
$('#menu a.active').removeClass('active');
$(this).addClass('active');
});
});
CSS:
.active
{
color:#32c0ce !important;
}
.active .filet_menu
{
border-bottom: 2px solid #32c0ce;
padding-bottom:2px;
}
$(".filet_menu", this).each(function () {
$(this).replaceWith(this.childNodes);
});
http://jsfiddle/LMm28/
On the other hand it may be easier to just have the span there the whole time and just add/remove the .filet_menu
class.
本文标签: javascriptHow to remove span inside an elementStack Overflow
版权声明:本文标题:javascript - How to remove span inside an element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743986684a2513981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论