繁体   English   中英

在div内的href周围包裹标签

[英]Wrap a tag around href inside a div

在HTML代码中有一个'href',是否有可能在其周围包装A-tag()? 我是新来的,所以请不要太苛刻:)

请注意,jQuery可以在div中查找子级的“ href”并将该属性设置为.summary-item-wrapper

HTML:

<div class="summary-item-wrapper" href="www.google.no" id="yui_3_17_2_4_1483527702805_1738"><div>

jQuery:

$(document).ready(function(){
  $('body').wrapInner('<div id="support"></div>');

  $('#support .sqs-block-summary-v2 .summary-item').each(function () {
    var linkto = $(this).find('.summary-title a').attr('href');
   $(this).children('.summary-item-wrapper').attr('href', linkto);   

  });
});

如果您的页面上有多个div,您希望进行转换并删除div,但要保留所有属性,则可以执行以下操作:

 $( "div.summary-item-wrapper" ).each(function() { $(this).before('<a href=http://'+$(this).attr('href') +'>A link'); $(this).prev().attr('id',$(this).attr('id')); $(this).prev().addClass($(this).attr('class')); }); $('div.summary-item-wrapper').remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="summary-item-wrapper" href="www.google.no" id="yui_3_17_2_4_1483527702805_1738">44444444444</div> <div class="summary-item-wrapper" href="www.google.com" id="yui_3_17_2_4_1483527702805_33333">ttttttttttt</div> 

您可以用以下方法完成:

$('#support .sqs-block-summary-v2 .summary-item-wrapper').wrap(function () {
    return '<a href="' + $(this).attr('href') + '"></a>';
});

href="google.no"表示转到<currentdomain>/google.no ,以防您需要google.no使用href="https://google.no"

检查JSFiddle

请也考虑对您的问题的评论。

暂无
暂无

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

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