如何检查DIV是否包含带有相对src路径的IMG标签?

编程入门 行业动态 更新时间:2024-10-27 07:26:41
本文介绍了如何检查DIV是否包含带有相对src路径的IMG标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将一个HTML页面拉到包含各种标签(img,div等)的服务器上.

I'm pulling a HTML page a server which contains various tags (img, div etc.).

在客户端,我将其插入div中,以使其完全像html文件一样显示.它可能包含图像和文本.但是有时有些img标签的src具有相对路径.

On the client side, I'm inserting this into a div so that it is getting displayed exactly like the html file. It may contain images and text. But sometimes there are img tags whose src has a relative path.

如何识别这些未引用完整http/https(绝对路径)而是以"/"开头的img标签,以标识它是相对的?

How can I identify these img tags which don't reference a full http/https (absolute path) but rather start with "/" so as to identify that it is relative?

推荐答案

  • 您可以使用 .find() 方法来检查div是否包含img
  • 您可以使用 .attr() 方法来获取src
    • You can use .find() method to check if the div contains img
    • You can use .attr() method to grab the src
    • 您可以使用字符串函数/正则表达式检查src属性是否以http,https或/开头并相应地进行更改.

      You can use string functions/regex to check if src attribute begins with http, https or / and change it accordingly.

      示例:

      $("#div1").find("img").length // returns the number of images inside #div1 $("#div1").find("img").each(function () { var src = $(this).attr("src"); // grab the src "attribute" console.log(src); console.log(src.indexOf("/") == 0); // true -> starts with / console.log(src.indexOf("") == 0); // true -> starts with console.log(src.indexOf("") == 0); // true -> starts with });

更多推荐

如何检查DIV是否包含带有相对src路径的IMG标签?

本文发布于:2023-10-29 20:28:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   标签   DIV   src   IMG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!