在 div 内的 href 周围包裹一个标签



在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。

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

最新更新