2015-10-19 34 views
-5

改变字符串IMG与价值如何去除img标签,并与ALT如何在ALT

var data="how are you <img src='blabla/emot/smile.gif' alt=':)'> yes <img src='blabla/emot/smile.gif' alt=':)'> <img src='blabla/emot/melet.gif' alt=':P'>"; 

的值来替换它,最终结果

var data="how are you :) yes :) :P"; 

我想消除img标签并用alt中的值替换,使用jQuery或javascript

+3

您能否将正文文本翻译成英文? – Script47

+0

你如何生成代码?我在这里看不到真正的问题。 – Thargor

回答

0

jQuery单线程很简单:

var data = "how are you <img src='blabla/emot/smile.gif' alt=':)'> yes <img src='blabla/emot/smile.gif' alt=':)'> <img src='blabla/emot/melet.gif' alt=':P'>"; 

var result = 
    $('<div>').html(data)     // create `<div>` and put data as contents 
       .find('img')    // find `<img>` elements in `<div>` 
       .replaceWith(function() { // replace `<img>` elements 
        return this.alt;  // with their `alt` attributes 
       }) 
       .end()      // return back to `<div>` element 
       .text();     // get text contents 

console.log(result); // "how are you :) yes :) :P"