jquery将strike()方法应用于object

时间:2014-07-07 08:37:06

标签: javascript

我可以通过以下方式获取所选字符串值,

    if(!window.Kolich){
      Kolich = {};
    }
    Kolich.Selector = {};
    Kolich.Selector.getSelected = function(){
      var t = '';
      if(window.getSelection){       
        t = window.getSelection();      
      } 
      return t;
    }
    Kolich.Selector.mouseup = function(){
     var st = Kolich.Selector.getSelected();
  // alert(typeof(st)) //showing object
     var data = st.toString();
     data.strike();
    }

但我需要对所选字符串应用strike()方法。

1 个答案:

答案 0 :(得分:0)

请尝试使用DOM Range strike()方法,而不是使用surroundContents()方法。

if(!window.Kolich){
  Kolich = {};
}

Kolich.Selector = {};
Kolich.Selector.getSelected = function(){
  var t = '';
  if(window.getSelection){       
    t = window.getSelection();      
  } 
  return t;
}

Kolich.Selector.mouseup = function(){
  var st = Kolich.Selector.getSelected();
  // alert(typeof(st)) //showing object
  //var data = st.toString();    
  var range = st.getRangeAt(0);    
  var strike = document.createElement("strike");    

  range.surroundContents(strike);    
}

演示:http://jsfiddle.net/F8saX/