简体   繁体   English

如何使用jQuery或javascript编辑父标记的html?

[英]How to edit html of parent tag using jQuery or javascript?

I'm using Shopify to host my online store, and have added some images to a page which I would like to be static. 我正在使用Shopify托管我的在线商店,并且已经向页面添加了一些图像,这些图像我希望保持不变。

Unfortunately Shopify wants to turn these into lightboxes using fancybox, and it is turning the image I supply into the child of a link tag. 不幸的是,Shopify希望使用fancybox将其转换为灯箱,并将我提供的图像转换为link标签的子级。 So in the code that I can edit, I see this: 因此,在我可以编辑的代码中,我看到了:

<div style="text-align: center;">
 <img src="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" alt="elle decoration magazine" style="display: inline-block; padding: 0 25px;"/>
</div>

but when I load the page it has done this: 但是当我加载页面时,它已经做到了:

<div style="text-aling: center;">
 <a href="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" class="fancyboximg">
  <img src="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" alt="elle decoration magazine" style="display: inline-block; padding: 0 25px;"/>
 </a>
</div>

I would rather not edit the original file that is causing this behaviour, because it's being used site-wide, and I know I will cause something else to stop working as intended if I do. 我宁愿不编辑导致此行为的原始文件,因为它在整个站点范围内使用,而且我知道,如果这样做,我会导致其他事情停止按预期工作。

Is it possible to remove the link tag retrospectively? 是否可以追溯删除链接标签?

Thanks for any suggestions! 感谢您的任何建议!

Use .unwrap() jQuery function. 使用.unwrap() jQuery函数。

Please check below snippet. 请检查以下片段。

 $(document).ready(function(){ $('.fancyboximg > #remove-fancy').unwrap(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="text-aling: center;"> <a href="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" class="fancyboximg"> <img id="remove-fancy" src="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" alt="elle decoration magazine" style="display: inline-block; padding: 0 25px;"/> </a> <a href="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" class="fancyboximg"> <img src="//cdn.shopify.com/s/files/1/1333/8187/t/6/assets/ASI_elle_decoration.jpg?9949612793051485599" alt="elle decoration magazine" style="display: inline-block; padding: 0 25px;"/> </a> </div> 

You can try with the jQuery unwrap function. 您可以尝试使用jQuery展开功能。

var imgTags = $( "img" );    
if ( imgTags.parent().is( "a" ) ) { imgTags.unwrap(); }

Link to jQuery doc 链接到jQuery文档

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

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