1. <i id='j9AK6'><tr id='j9AK6'><dt id='j9AK6'><q id='j9AK6'><span id='j9AK6'><b id='j9AK6'><form id='j9AK6'><ins id='j9AK6'></ins><ul id='j9AK6'></ul><sub id='j9AK6'></sub></form><legend id='j9AK6'></legend><bdo id='j9AK6'><pre id='j9AK6'><center id='j9AK6'></center></pre></bdo></b><th id='j9AK6'></th></span></q></dt></tr></i><div id='j9AK6'><tfoot id='j9AK6'></tfoot><dl id='j9AK6'><fieldset id='j9AK6'></fieldset></dl></div>

        <bdo id='j9AK6'></bdo><ul id='j9AK6'></ul>
      <legend id='j9AK6'><style id='j9AK6'><dir id='j9AK6'><q id='j9AK6'></q></dir></style></legend>
      <tfoot id='j9AK6'></tfoot>
    1. <small id='j9AK6'></small><noframes id='j9AK6'>

    2. 使用 javascript/jQuery 从 HTML 字符串中获取属性值

      时间:2023-09-08
        <bdo id='1emDU'></bdo><ul id='1emDU'></ul>
            <i id='1emDU'><tr id='1emDU'><dt id='1emDU'><q id='1emDU'><span id='1emDU'><b id='1emDU'><form id='1emDU'><ins id='1emDU'></ins><ul id='1emDU'></ul><sub id='1emDU'></sub></form><legend id='1emDU'></legend><bdo id='1emDU'><pre id='1emDU'><center id='1emDU'></center></pre></bdo></b><th id='1emDU'></th></span></q></dt></tr></i><div id='1emDU'><tfoot id='1emDU'></tfoot><dl id='1emDU'><fieldset id='1emDU'></fieldset></dl></div>

                <tbody id='1emDU'></tbody>

              <legend id='1emDU'><style id='1emDU'><dir id='1emDU'><q id='1emDU'></q></dir></style></legend>

            1. <small id='1emDU'></small><noframes id='1emDU'>

              1. <tfoot id='1emDU'></tfoot>
                本文介绍了使用 javascript/jQuery 从 HTML 字符串中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个包含文本和图像的 HTML 字符串,例如

                I have an HTML string that contains text and images, e.g.

                <p>...</p>
                <img src="..." />
                <p>...</p>
                

                我需要获取第一张图片的 src 属性.图片可能出现在 <p> 之后,也可能不出现,并且可能有多个图片,或者根本没有图片.

                I need to get the src attribute of the first image. The image may or may not come after a <p> and there could be more than one image, or no image at all.

                最初,我尝试将字符串附加到 DOM 并进行过滤.但是,如果我这样做,浏览器会请求所有外部资源.在我的情况下,这会增加很多不必要的开销.

                Initially, I tried appending the string to the DOM and filtering. But, if I do this the browser requests all of the external resources. In my situation, this adds a lot of unnecessary overhead.

                我最初的做法:

                var holder = $('<div></div>'); 
                
                holder.html(content);
                
                var src = holder.find('img:first').attr('src'); 
                

                如何在不附加 HTML 的情况下获取第一张图片的 src?我需要使用正则表达式,还是有更好的方法?

                How can I get the src of the first image without appending the HTML? Do I need to use a regular expression, or is there a better way?

                解决方案需要基于 javascript/jQuery——我没有使用任何服务器端语言.

                The solution needs to be javascript/jQuery based – I'm not using any server side languages.

                我的问题非常类似于:http://forum.jquery.com/topic/parsing-html-without-retrieving-external-resources

                谢谢

                推荐答案

                这个

                $("<p><blargh src='whatever.jpg' /></p>").find("blargh:first").attr("src")
                

                返回 whatever.jpg 所以我想你可以试试

                returns whatever.jpg so I think you could try

                $(content.replace(/<img/gi, "<blargh")).find("blargh:first").attr("src")
                

                编辑

                / 而不是 "<img"

                这篇关于使用 javascript/jQuery 从 HTML 字符串中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tfoot id='0mERX'></tfoot>

                  1. <small id='0mERX'></small><noframes id='0mERX'>

                    <i id='0mERX'><tr id='0mERX'><dt id='0mERX'><q id='0mERX'><span id='0mERX'><b id='0mERX'><form id='0mERX'><ins id='0mERX'></ins><ul id='0mERX'></ul><sub id='0mERX'></sub></form><legend id='0mERX'></legend><bdo id='0mERX'><pre id='0mERX'><center id='0mERX'></center></pre></bdo></b><th id='0mERX'></th></span></q></dt></tr></i><div id='0mERX'><tfoot id='0mERX'></tfoot><dl id='0mERX'><fieldset id='0mERX'></fieldset></dl></div>

                        <bdo id='0mERX'></bdo><ul id='0mERX'></ul>
                        <legend id='0mERX'><style id='0mERX'><dir id='0mERX'><q id='0mERX'></q></dir></style></legend>