【相关学习推荐:javascript视频教程】
写之前在网上找了很多方法,最简单的思路应该是1.获取ueditor中的内容;2.将获取到的字符串转换成jquery对象;3.选择器找到img元素,获取src值。
var content= ue.geteditor('details').getcontent();//获取编辑器内容var $p = document.createelement("p");//创建一个p元素对象$p.innerhtml = content;//往p里填充htmlvar $v = $($p);//从dom对象转换成jquery对象$.each($v.find("img"),function (v,i) {//选择器找到img元素,循环获取src值console.log("src======" i.src);});打印结果:
写出上面代码之前碰了几次壁,绕了几个弯,下面就是我整个开发过程,记录下。
1.获取ueditor中的内容
这一步很简单,使用编辑器提供的getcontent()函数
2.将获取到的字符串转换成jquery对象
<p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255);"> 夏季到了,持续高温就连大人都受不了,更别说孩子了。所以该不该给孩子穿袜子又成了宝妈心头的大事,一方面觉得应该给孩子穿,毕竟这个几个理由是拒绝不了的。 </p> <p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255); text-align: center;"> <img alt="1.jpg" width="490" height="306" src="http://www.socksb2b.com/d/file/zixun/wazichangshi/2019-07-05/1b0038e6cf808ae9c091c34ded031de9.jpg" _src="http://www.socksb2b.com/d/file/zixun/wazichangshi/2019-07-05/1b0038e6cf808ae9c091c34ded031de9.jpg"> </p> <p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255);"> 还有一部分宝妈意见不同,认为还是不穿袜子比较好: </p> <p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255);"> 1.小孩子经常出汗,新陈代谢比较快,袜子如果不透气的话,有可能会因为生脚汗导致孩子哭闹不休。 </p> <p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255);"> 2.脚底的穴位多,不穿袜子可以充分按摩到脚底。有利于身体其他机能的运转。缓解便秘,消化不良等症状。 </p> <p style="margin-top: 1em; margin-bottom: 1em; white-space: normal; box-sizing: border-box; padding: 0px; border: 0px; vertical-align: middle; line-height: 25px; list-style: none; color: rgb(58, 58, 58); font-family: 微软雅黑, 宋体, verdana, arial, helvetica, sans-serif; font-size: 14px; background-color: rgb(247, 253, 255);"> 好像两方家长说的都有道理,那么到底应该穿袜子吗? </p>var content= ue.geteditor(‘details').getcontent();
上面是我编辑器里的内容(content),最简单的方法是用
$(content)来转换成jquery对象,但是$(content).html()的打印结果如下:
可以看出来转换出的jquery对象代表的是content中第一个html元素p,剩下的html元素获取不到,也就无法进行第三步获取图片地址。
这里可以补充的是,网上提供的一种方法
$(content).get(0).outerhtml的打印结果如下:
get(1)、get(2)…依次可以打印出接下来的html元素代码,我开始考虑循环获取,但是循环次数的获取回到了原地,根本取不到,有兴趣的可以尝试。
既然jquery的思路断了,我就开始考虑原生js的方法,在网上找了个:
var $p = document.createelement("p");//创建一个p元素对象$p.innerhtml = content;//往p里填充html打印出来的结果非常好:
前面绕的弯两行代码就解决了,原生js真棒!
但是我还是习惯用jquery,又把它转换成jquery了,方便下面的选择器和循环
var $v = $($p);//从dom对象转换成jquery对象
3.选择器找到img元素,获取src值
$.each($v.find("img"),function (v,i) {console.log("src======" i.src);});i.src可以直接获取图片url地址,成功!
下面为大家补充
js如何获取ueditor里面的第一张图片
想获取ueditor里面第一张图片
怎么购买二手域名?购买二手域名要注意哪些购买了一个阿里云的服务器阿里云租服务器扩容建设购物网站以实现转化为主域名转出操作不能执行-域名及账户问题微商创业如何能少走弯路?阿里云服务器怎么备案号云服务器和云租赁费用