duai3681 2017-07-11 14:45
浏览 52
已采纳

删除HTML中的部分标记

I noticed a typo in multiple HTML tags in my DOM. Chrome and Edge browser ignores the typo and loads the image however Internet Explorer does not load the image at all.

Is there a script I can apply to remove the src="<img part of the tag?

<a href="https://www.prikkabelled.nl/feestverlichting-voor-de-achtertuin/"><img src="<img width="806" height="605" src="https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin.jpg 806w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-152x114.jpg 152w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-768x576.jpg 768w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-253x190.jpg 253w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-506x380.jpg 506w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-600x450.jpg 600w, https://www.prikkabelled.nl/wp-content/uploads/Feestverlichting-voor-in-de-achtertuin-200x150.jpg 200w" sizes="(max-width: 806px) 100vw, 806px" /></a>

I know how to get all the elements using:

jQuery( "img.attachment-post-thumbnail" );

But now I am wondering how to remove the src="<img from each element in the array returned.

  • 写回答

1条回答 默认 最新

  • douqiao7958 2017-07-11 14:49
    关注

    there are a few ways that can achieve this:

    method 1

    .removeAttr() - removes attribute based on what's passed. e.g. $('.class').removeAttr('src')

    method 2

    using a $.each to loop through then using $(this)

    $('.class').each(function()
    {
        $(this).removeAttr('src')
    })
    

    method 3

    using .attr - you can use this to set and remove attributes like so:

    $('.class').attr('key', 'value');
    //e.g.
    $('.class').attr('src', 'www.google.com')
    //or more for you
    $('.class').attr('src', '')
    

    I think you can use .prop in the same was .attr

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面