在div innerHTML中用alt替换所有img标签

时间:2014-03-23 14:12:36

标签: javascript replacewith

我有这段代码:

var str = document.getElementById('mesajyazi').innerHTML;
alert('input: '+str);

// first create an element and add the string as its HTML
var container = $('<div>').html(str);

// then use .replaceWith(function()) to modify the HTML structure
container.find('img').replaceWith(function() { return this.alt; })

// finally get the HTML back as string
var strAlt = container.html();
alert('output: '+strAlt);

它没有用。

但是,如果我将var str更改为简单的字符串文本。

var str = 'This is a string with <img src="./images/logo.png" alt="logo" /> an image';

有效。

P.S。 - 受此Replace all <img> tag with img alt text启发:)

0 个答案:

没有答案
相关问题