2013-05-13 27 views
1
'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">,這可能嗎?請幫幫我。如何添加ALT在JavaScript的execCommand梅索德的圖片src標籤?

回答

1

看來,你不能用的execCommand直接Documentation of the commandsan other one做到這一點。

你仍然可以做它的jQuery,添加圖像後(似乎有點hackish,我知道,但不認爲你可以做的更好):

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

我發現的execCommand()在DOM實際更新之前返回。所以@MisterJ的建議需要一些邏輯來確保在通過JQuery添加'alt'屬性之前存在新的img元素。 – joeymink 2016-02-11 16:07:19

+0

想你可以在CONTENTEDITABLE DOM元素上使用MutationObserver:[文件](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) – MisterJ 2016-02-12 09:37:41