如何在javascript execCommand方法中添加alt到图像src标签?

时间:2013-05-13 09:28:13

标签: javascript jquery html5

'insert_img': { 
    "text": "Inserimage", 
    "icon":"icon icon-picture", 
    "tooltip": "Insertimage", 
    "commandname":null, 
    "custom": function(){ 
        var imgSrc = prompt('Enter image location', '');
        if (imgSrc != null) {
            document.execCommand('insertimage', false, imgSrc);
        } 
    } 
},

我有这样的代码,我想给图像添加alt,我该怎么做到这一点?当我拿这个来源我想看到<img src="smiley.gif" alt="Smiley face">这样的时候,有可能吗?请帮帮我。

2 个答案:

答案 0 :(得分:1)

您似乎无法直接使用execCommand Documentation of the commands an other one执行此操作。

在添加图像之后你仍然可以在jQuery中执行它(看起来有点hackish,我知道,但不认为你可以做得更好):

$('img[src="smiley.gif"]').attr('alt','Smiley face');

答案 1 :(得分:0)

您可以像这样使用insertHTML命令:

document.execCommand('insertHTML', 0, '<img src="' + newUrlValue + '" alt="' + newAltValue + '" />');