jquery - JQuery 在点击时环绕文本
问题描述
我有一个小菜单,html
点击后我可以将 -files 加载到 div 中。我希望在单击时将菜单项包装在跨度(或 div)中,以便我可以控制样式。如果单击另一个菜单项,则跨度应该消失并放在另一个单击的菜单项周围。
我对此很迷茫,除了下面的片段之外,我不太确定如何处理这个问题。此外,必须包含在 a 中的文本<span>
可能会有所不同。
请看片段。
$(function() {
$('#om').click(function(e) {
$('.hestetekst').load('html/tekst.shtml');
e.preventDefault();
});
$('#resul').click(function(e) {
$('.hestetekst').load('html/resul.html');
e.preventDefault();
});
$('#billeder').click(function(e) {
$('.hestetekst').load('html/billeder.html');
e.preventDefault();
});
$('#video').click(function(e) {
$('.hestetekst').load('html/video.html');
e.preventDefault();
});
$('#afkom').click(function(e) {
$('.hestetekst').load('html/afkom.html');
e.preventDefault();
});
$('#presse').click(function(e) {
$('.hestetekst').load('html/presse.html');
e.preventDefault();
});
});
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.overrideMimeType('text/html; charset=ISO-8859-1');
}
});
$(document).on('click', '.hesteundertop a', function(e) {
$(this).parent().siblings().removeClass('active');
$(this).parent().addClass('active');
});
.active a {
background: green;
width: auto;
color: grey;
}
.hesteundertop a:link {
text-decoration: none;
color: #2E181A;
display: inline-block;
}
.hesteundertop a:visited {
text-decoration: none;
color: #2E181A;
display: inline-block;
}
.hesteundertop a:active {
text-decoration: none;
color: grey;
display: inline-block;
}
.hesteundertop a:hover {
text-decoration: underline;
color: grey;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hesteundertop">
<p><a id="om" href="#">Om Corona</a> | <a id="resul" href="#">Resultater</a> | <a id="billeder" href="#">Billeder</a> | <a id="video" href="#">Video</a> | <a id="afkom" href="#">Afkom</a> | <a id="presse" href="#">Presse</a></p>
</div>
<div class="hestetekst">
<!--#include virtual="html/tekst.shtml"--><br />
<hr class="hr75" />
</div>
解决方案
$('.hesteundertop .active a').unwrap( )
$(this).wrap("<span class='active'></span>");
上面的代码将用 span 包装单击的链接并将其解包。
我想这就是你要找的。
$(function() {
$('#om').click(function(e) {
$('.hestetekst').load('html/tekst.shtml');
e.preventDefault();
});
$('#resul').click(function(e) {
$('.hestetekst').load('html/resul.html');
e.preventDefault();
});
$('#billeder').click(function(e) {
$('.hestetekst').load('html/billeder.html');
e.preventDefault();
});
$('#video').click(function(e) {
$('.hestetekst').load('html/video.html');
e.preventDefault();
});
$('#afkom').click(function(e) {
$('.hestetekst').load('html/afkom.html');
e.preventDefault();
});
$('#presse').click(function(e) {
$('.hestetekst').load('html/presse.html');
e.preventDefault();
});
});
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.overrideMimeType('text/html; charset=ISO-8859-1');
}
});
$(document).on('click', '.hesteundertop a', function(e) {
$('.hesteundertop .active a').unwrap( )
$(this).wrap("<span class='active'></span>");
});
.active a {
background: green;
width: auto;
color: grey;
}
.hesteundertop a:link {
text-decoration: none;
color: #2E181A;
display: inline-block;
}
.hesteundertop a:visited {
text-decoration: none;
color: #2E181A;
display: inline-block;
}
.hesteundertop a:active {
text-decoration: none;
color: grey;
display: inline-block;
}
.hesteundertop a:hover {
text-decoration: underline;
color: grey;
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hesteundertop">
<p><a id="om" href="#">Om Corona</a> | <a id="resul" href="#">Resultater</a> | <a id="billeder" href="#">Billeder</a> | <a id="video" href="#">Video</a> | <a id="afkom" href="#">Afkom</a> | <a id="presse" href="#">Presse</a></p>
</div>
<div class="hestetekst">
<!--#include virtual="html/tekst.shtml"--><br />
<hr class="hr75" />
</div>
推荐阅读
- python - 我想获得 Entrybox 和 Checkbutton 的值,但我什么也没得到,为什么?(我是 tkinter 的新手)
- php - 如何保存最小值价格?
- hdf5 - HDFql 获取组的大小
- c++ - 使(剥离的)DLL 无法与 C++ 控制台应用程序一起使用
- c++ - 数组变量周围的堆栈损坏
- xamarin - Xamarin.ios 应用程序在 HttpClient 异步请求上崩溃(SIGABRT:未连接套接字)
- c# - 如何捕捉元素是否不可见
- python - 乘法和重复的星号
- fibonacci - 斐波那契数列,公共静态 void xxx
- c++ - 在多线程程序中使用 libcurl 时获取信号 11 (SIGSEGV)