简体   繁体   English

在div内的href周围包裹标签

[英]Wrap a tag around href inside a div

In the HTML code there is a 'href' , is there any posiblity to wrap an A-tag() around it? 在HTML代码中有一个'href',是否有可能在其周围包装A-tag()? I'm new to this so please don't be too harsh :) 我是新来的,所以请不要太苛刻:)

Note that the jquery is there to find the 'href' of a child inside the div and setting that attritbute to .summary-item-wrapper 请注意,jQuery可以在div中查找子级的“ href”并将该属性设置为.summary-item-wrapper

HTML: HTML:

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

Jquery: 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);   

  });
});

If there are multiple divs on your page you wish to convert, and to remove divs, but to keep all attributes, you can do something like this: 如果您的页面上有多个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> 

What you want can be done with this: 您可以用以下方法完成:

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

Also href="google.no" means go to <currentdomain>/google.no , in case you need the google.no use href="https://google.no" href="google.no"表示转到<currentdomain>/google.no ,以防您需要google.no使用href="https://google.no"

Check JSFiddle . 检查JSFiddle

Please consider the comments on your question too. 请也考虑对您的问题的评论。

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

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