如何在tiny mce中查找用户是否选择了整个文本或部分文本
我想检查所选文本的节点。如果它是跨度,另一个函数将编辑应用于它的类。如果它与父节点相同,则代码会将所选内容包装在 span 中并设置其样式。我面临的问题是,如何确定用户是否选择了编辑器中的整个文本或仅选择了部分文本。如果用户选择整个文本,我想将样式应用到父节点,而不是向其添加新的跨度和样式。下面是我的代码 -
var ed=tinyMCE.getInstanceById('textAreaId');
var sel = ed.selection.getContent();
if(trim(sel)!="") {
//get the node of the selection
var thisNode=tinyMCE.activeEditor.selection.getStart().nodeName;
ed_Property=propertyName;
ed_PropertyVal=propertyValue;
//get the root node inside editor
var parentNode=tinyMCE.activeEditor.getBody().firstChild.nodeName;
if(thisNode=="SPAN") {
nodeclass=$(tinyMCE.activeEditor.selection.getNode()).attr('class');
//edit span properties
editSpanProperties(nodeclass,propertyName,propertyValue);
}
else if(thisNode==parentNode) {
var elmType="span";
var Spanid1=createSpanId(targetToStyle,elmType);
Spanid=targetToStyle+"_"+elmType+"_"+Spanid1;
var strStyle=ed_Property+":"+ed_PropertyVal;
//wrap selection in a span
sel = "<span id='"+Spanid+"' style='"+strStyle+"'>" + sel + "</span>";
//set content
ed.selection.setContent(sel);
//retain the selection
ed.selection.select(ed.dom.select('#'+Spanid)[0],false);
}
else {
//here I need to check if user has selected whole text and set properties
setStylesOnEditor(templateSection,propertyName,propertyValue);
}//if ends
}
else if(trim(sel)=="") {
alert('No text selected');
}
I want to check the node of the selected text. If it is span, another function will edit the classes applied to it. If it is same as the parent node, then the code will wrap the selection in span and set its styles. The problem here I'm facing is, how to determine, if user has selected the whole text inside the editor or only some text. If user selects the whole text, I want to apply the styles to the parent node instead of adding a new span and styles to it. Below is my code -
var ed=tinyMCE.getInstanceById('textAreaId');
var sel = ed.selection.getContent();
if(trim(sel)!="") {
//get the node of the selection
var thisNode=tinyMCE.activeEditor.selection.getStart().nodeName;
ed_Property=propertyName;
ed_PropertyVal=propertyValue;
//get the root node inside editor
var parentNode=tinyMCE.activeEditor.getBody().firstChild.nodeName;
if(thisNode=="SPAN") {
nodeclass=$(tinyMCE.activeEditor.selection.getNode()).attr('class');
//edit span properties
editSpanProperties(nodeclass,propertyName,propertyValue);
}
else if(thisNode==parentNode) {
var elmType="span";
var Spanid1=createSpanId(targetToStyle,elmType);
Spanid=targetToStyle+"_"+elmType+"_"+Spanid1;
var strStyle=ed_Property+":"+ed_PropertyVal;
//wrap selection in a span
sel = "<span id='"+Spanid+"' style='"+strStyle+"'>" + sel + "</span>";
//set content
ed.selection.setContent(sel);
//retain the selection
ed.selection.select(ed.dom.select('#'+Spanid)[0],false);
}
else {
//here I need to check if user has selected whole text and set properties
setStylesOnEditor(templateSection,propertyName,propertyValue);
}//if ends
}
else if(trim(sel)=="") {
alert('No text selected');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将所选文本与编辑器内容进行比较:
You need to compare the selected text with the editors content: