如何利用JQuery 替换HTML字符串中的属性值呢?

如 html 字符串有很多 img标签,现在需要修改 img的src值

var html="<div style="text-align:center;font-size:40px;font-family:'方正小标宋简体'">  期末测试</div>
<p class="title" id="ab"> 一、选择题(共 20 题)</p>
<div>
<p>1.下列软件中可以查看WWW信息的是______。 23</p>
<p><img src="../UploadFiles/defa43e0-f176-407e-a44b-6c46af9ad04c.jpg" alt="" width="400" height="225"></p>
<p>&nbsp;</p>
</div>
<div>
A.游戏软件<img src="../UploadFiles/0440377a-5fa2-41fe-813c-e1ead615da16.JPG" alt="" width="163" height="204"> &nbsp;&nbsp; B.财务软件 &nbsp;&nbsp; C.杀毒软件 &nbsp;&nbsp; D.浏览器软件
</div>
<div>
<p>2.在拨号入网时,______不是必备的硬件。</p>
<p><img src="../UploadFiles/88fef814-5140-4ea5-b4b2-7fdeacda8c22.jpg" alt="" width="425" height="283"></p>
</div>
<div><img src="../UploadFiles/be7d51d8-5a2f-4f6d-9cdf-32517386934e.jpg" alt="" width="300" height="400"> </div>";

如何替换呢? 以下代码理论上可以,但实际不能替换变量 html 字符串中的 dom元素,因为以下操作方式并不是引用传值。只有将操作的字符串变成对象才能发生实际改变。

一、理论上修改方式

 var html = $("#qst").html();

           var s= $("img",html).attr("src")
$("img", $(html)).each(function (a, b) {
var pos = $(this).attr('src').indexOf("/");
s = $(this).attr('src').substring(pos);
var newImg= $(this).attr('src', "../wwwroot/" + s);
$(this).replaceWith("<img src='" + "../wwwroot/" + s+"' />");
console.log($(this));
});
alert(html);

二、实际修改方式

废话不说上代码:

  var html = $("#qst").html();
alert(html);
var $html = $('<div />', { html: html });//关键点 将html字符串转换成 JavaScript 对象。 $html.find("img").each(function () {
var pos = $(this).attr('src').indexOf("/");
s = $(this).attr('src').substring(pos);
var newImg = $(this).attr('src', "../wwwroot/" + s);
});
alert($html.html());

请看调用前

调用后:

可以看到,Img 标签的所有 src 值 均发生改变。

end!!!

Jquery 如何替换html字符串中标签属性值 ??的更多相关文章

  1. 获取xml字符串中的属性值

    pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...

  2. 【JS新手教程】replace替换一个字符串中所有的某单词

    JS中的replace方法可以替换一个字符串中的单词.语句的格式是: 需要改的字符串.replace(字符串或正则表达式,替换成的字符串) 如果第一个参数用字符串,默认是找到该字符串中的第一个匹配的字 ...

  3. 【Python】获取翻页之后的各页面中的属性值。

    如何获取翻页之后的页面中的html标签中的属性值? # coding=utf-8 from selenium import webdriver if __name__=="__main__& ...

  4. <s:property="a" value=""/>取的<s:debug></s:debug>中的value stack中的属性值

    <s:property="a"  value=""/>取的<s:debug></s:debug>中的value stack中 ...

  5. 将source类中的属性值赋给target类中对应的属性

    /** * 对象的属性值拷贝 * <p> * 将source对象中的属性值赋值到target对象中的属性,属性名一样,类型一样 * <p> * example: * <p ...

  6. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

  7. jquery 取子节点及当前节点属性值

    分享下jquery取子节点及当前节点属性值的方法. <li class="menulink"><a href="#" rel="ex ...

  8. ajax取到数据后如何拿到data.data中的属性值

    今天遇到的ajax取到数据后如何拿到data.data中的属性值的问题 比如拿到了数据 我要取出data中的name 题外话:当然取名最好别取什么奇怪的xiaobi

  9. Implement Property Value Validation in the Application Model 在应用程序模型中实现属性值验证

    In this lesson, you will learn how to check whether or not a property value satisfies a particular r ...

  10. js和jquery通过this获取html标签中的属性值

    <html> <head> <script type="text/javascript" src="jquery-1.10.2.min.js ...

随机推荐

  1. 面了几个说自己精通 Vue 的同学,实在一言难尽……

    请说一下响应式数据的原理 默认 Vue 在初始化数据时,会给 data 中的属性使用 Object.defineProperty 重新定义所有属性,当页面到对应属性时,会进行依赖收集 (收集当前组件中 ...

  2. ant design vue datePicker 日期时间范围设置默认为当天0点-23点59

    <template> <a-card :bordered="false"> <a-range-picker show-time :default-va ...

  3. css3的的新特性

    1.transform 2.calc 3.transition

  4. SAP 交货单抬头客制化界面以及字段

    需求 因为涉及系统之间的家伙但传输,方便业务查看该交货单是否已经发送,因此再交货单的抬头页面进行增强,给出一个是否发送的字段显示.如下: 业务加载项 LE_SHP_TAB_CUST_HEAD se19 ...

  5. js数字取整的方法

    parseInt(123.34)=123(保留整数) Math.ceil(123.34)=124(向上取整) Math.floor(123.34)=123(向下取整) Math.round(123.3 ...

  6. 学习Typora免费安装步骤

    学习Typora免费安装步骤 第一步: 安装typora-setup-x64-1.2.4.exe, 注意版本 第二步: 替换掉resources目录下的app.asar文件 第三步: 重启Typora ...

  7. go环境 依赖管理 基本命令

    Go安装 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ Windows 选择Windows版本下载安 ...

  8. C#中实现类型对foreach的支持

    代码实现: 首先创建用来遍历的类 class Car { public string name; public int age; } public class Cars: IEnumerable { ...

  9. 禁止Edge升级

    管理员命令行运行: 用法:EdgeChromium_Blocker.cmd [<machine name>] [/B][/U][/H] 1.本机禁用更新: EdgeChromium_Blo ...

  10. LCP 03.机器人大冒险

    def robot(command, obstacles, x, y): xx = 0 yy = 0 tmp = [] for c in command: if c == 'U': yy += 1 i ...