【问题标题】:How to find if user has selectedwhole text or a part of text in tiny mce如何查找用户是否在微型 mce 中选择了整个文本或部分文本
【发布时间】:2010-11-23 16:19:21
【问题描述】:

我想检查所选文本的节点。如果它是跨度,另一个函数将编辑应用于它的类。如果它与父节点相同,则代码会将选择包装在 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');
  }

【问题讨论】:

    标签: tinymce selection range


    【解决方案1】:

    如果用户选择了如何确定 编辑器内的整个文本或 只有一些文字。

    您需要将所选文本与编辑器内容进行比较:

    var content_editor = $(ed.getBody()).text();
    
    var content_selection = ed.selection.getContent({format : 'text'});
    
    // now compare both either characterwise or as whole string, 
    // you might need to strip whitespace characters from the strings!
    // and do what you want to do in each case
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      相关资源
      最近更新 更多