在 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>
J查询:
$(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>
关于javascript - 在 div 中围绕 href 包裹标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462442/