如何将样式属性添加到使用getDoc().createElement("img")添加的图像中;在TinyMCE [英] How to add style attribute to an image added with getDoc().createElement( "img" ); in TinyMCE
问题描述
根据上述问题,我设法在TinyMCE中添加了一个图片.
From the above mentioned question I manage to add an Image in TinyMCE.
var ed = tinyMCE.get('txt_area_id'); // get editor instance
var range = ed.selection.getRng(); // get range
var newNode = ed.getDoc().createElement ( "img" ); // create img node
newNode.src="sample.jpg"; // add src attribute
range.insertNode(newNode); // insert Node
我正在尝试使用以下代码将宽度添加到newNode
:
I am trying to add the width to newNode
with this code:
newNode.style = "width:600px;"; // not working
但是它不起作用,对于类也是如此,我无法通过此代码添加类:
but its not working, same goes for class I cannot add a class via this code:
newNode.class= "myClass"; // this one is also not working
如果有人有任何想法,请告诉我谢谢.
If any one have some idea please let me know thanks.
推荐答案
问题在这里:
newNode.style = "width:600px;";
您正在访问节点的样式对象,而不是样式属性.因此,您可以更新或设置样式对象:
You're accessing the node's style object, rather than the style attribute. So, you can either update, or set, the style object:
newNode.style.width = "600px;";
或更新或设置样式属性:
Or update, or set, the style attribute:
newNode.setAttribute("style", "width:600px");
请注意,在后一个示例中,style属性中保存的所有现有值都将被新字符串覆盖;要仅更新一个属性值,应使用前面的示例,并定位样式对象的特定属性.
Note that, in the latter example, any existing values held in the style attribute will be overwritten with the new string; to update only one property value you should use the former example, and target specific properties of the style object.
要更新元素的类,请执行以下操作:
To update the classes of an element:
newNode.className = "newClassName";
或者:
newNode.classList.add("newClassName");
这篇关于如何将样式属性添加到使用getDoc().createElement("img")添加的图像中;在TinyMCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!