2015-10-19 61 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"