简体   繁体   English

jQuery在单击时环绕文本

[英]JQuery wrap span around text on click

I have a little menu from which I load html -files into a div on click. 我有一个小菜单,单击后可以从其中加载html -files到div中。 I want menu items to be wrapped in a span (or div) when clicked upon so I can control the style. 我希望菜单项在单击时被包裹在一个跨度(或div)中,以便我可以控制样式。 If another menu item is clicked the span should disappear and be put around the other clicked menu item. 如果单击另一个菜单项,则跨度应消失并放在另一个单击的菜单项周围。

I'm pretty lost on this and I'm not quite sure on how to approach this other than in below snippet. 我对此很迷茫,除了下面的代码段外,我不太确定如何处理此问题。 Also the text that has to be wrapped in a <span> could differ. 另外,必须用<span>换行的文本可能会有所不同。

Please see snippet. 请参阅摘要。

 $(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> &#124; <a id="resul" href="#">Resultater</a> &#124; <a id="billeder" href="#">Billeder</a> &#124; <a id="video" href="#">Video</a> &#124; <a id="afkom" href="#">Afkom</a> &#124; <a id="presse" href="#">Presse</a></p> </div> <div class="hestetekst"> <!--#include virtual="html/tekst.shtml"--><br /> <hr class="hr75" /> </div> 

You can simply do wrap() and unwrap() 您可以简单地进行wrap()unwrap()

  $('.hesteundertop .active a').unwrap(  )
  $(this).wrap("<span class='active'></span>");

Above code will wrap clicked link with span and unwrap it. 上面的代码将单击的链接用span包裹并解开。

I think this is what you are looking for. 我认为这就是您想要的。

 $(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> &#124; <a id="resul" href="#">Resultater</a> &#124; <a id="billeder" href="#">Billeder</a> &#124; <a id="video" href="#">Video</a> &#124; <a id="afkom" href="#">Afkom</a> &#124; <a id="presse" href="#">Presse</a></p> </div> <div class="hestetekst"> <!--#include virtual="html/tekst.shtml"--><br /> <hr class="hr75" /> </div> 

You can clear active from all elements at once and then add it to current item. 您可以一次清除所有元素中的active元素,然后将其添加到当前项目中。

$('.hesteundertop a').on('click', function(e) {
  $('.hesteundertop a').removeClass('active');
  $(this).addClass('active');
});

Example on Codepen Codepen示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM