第一次单击时显示开始标记,第二次单击时关闭标记

时间:2015-10-19 21:15:08

标签: javascript tags

我有这个功能,将标签放入textarea。现在,它在点击时同时放置两个标签(打开和关闭)。

工作演示:http://jsfiddle.net/gyky8qca/1/

我希望它能够在一次点击时显示开始标记,在另一次点击时显示结束标记。因此,例如,如果点击了Bold,则应显示<b>,然后如果再次点击它,则应显示</b>。与其他按钮相同。

function addTagSel(tag, idelm) {
var tag_type = new Array('<', '>');        // for BBCode tag, replace with:  new Array('[', ']');
var txta = document.getElementById('wmd-input');
var start = tag_type[0] + tag + tag_type[1];
var end = tag_type[0] +'/'+ tag +  tag_type[1];
var IE = /*@cc_on!@*/false;    // this variable is false in all browsers, except IE

if (IE) {
var r = document.selection.createRange();
var tr = txta.createTextRange();
var tr2 = tr.duplicate();
tr2.moveToBookmark(r.getBookmark());
tr.setEndPoint('StartToEnd',tr2);
var tag_seltxt = start + r.text + end;
var the_start = txta.value.replace(/[\r\n]/g,'.').indexOf(r.text.replace(/[\r\n]/g,'.'),tr.text.length);
txta.value = txta.value.substring(0, the_start) + tag_seltxt + txta.value.substring(the_start + tag_seltxt.length, txta.value.length);

var pos = txta.value.length - end.length;    // Sets location for cursor position
tr.collapse(true);
tr.moveEnd('character', pos);        // start position
tr.moveStart('character', pos);        // end position
tr.select();                 // selects the zone
}
else if (txta.selectionStart || txta.selectionStart == "0") {
var startPos = txta.selectionStart;
var endPos = txta.selectionEnd;
var tag_seltxt = start + txta.value.substring(startPos, endPos) + end;
txta.value = txta.value.substring(0, startPos) + tag_seltxt + '\u200C' + txta.value.substring(endPos, txta.value.length);

// Place the cursor between formats in #txta
txta.setSelectionRange((endPos+start.length),(endPos+start.length));
txta.focus();
}
return tag_seltxt;
}
document.getElementById('big').onclick = function() {
var tag_seltxt = addTagSel('big');
return tag_seltxt;
}
document.getElementById('b').onclick = function() {
var tag_seltxt = addTagSel('b');
return tag_seltxt;
}
document.getElementById('i').onclick = function() {
var tag_seltxt = addTagSel('i');
return tag_seltxt;
}
document.getElementById('u').onclick = function() {
var tag_seltxt = addTagSel('u');
return tag_seltxt;
}
document.getElementById('del').onclick = function() {
var tag_seltxt = addTagSel('del');
return tag_seltxt;
};

1 个答案:

答案 0 :(得分:2)

如果此点击是开始标记或结束标签,则添加counter对象进行计数。

&#13;
&#13;
var counter = {}
function addTagSel(tag, idelm) {
  var tag_type = new Array('<', '>');        // for BBCode tag, replace with:  new Array('[', ']');
  var txta = document.getElementById('wmd-input');
  var start = tag_type[0] + tag + tag_type[1];
  var end = tag_type[0] +'/'+ tag +  tag_type[1];
  var IE = /*@cc_on!@*/false;    // this variable is false in all browsers, except IE
  counter[tag] = counter[tag] || 0;
  
  if (IE) {
    var r = document.selection.createRange();
    var tr = txta.createTextRange();
    var tr2 = tr.duplicate();
    tr2.moveToBookmark(r.getBookmark());
    tr.setEndPoint('StartToEnd',tr2);
    var tag_seltxt = counter[tag] ? r.text + end : start + r.text;
    var the_start = txta.value.replace(/[\r\n]/g,'.').indexOf(r.text.replace(/[\r\n]/g,'.'),tr.text.length);
    txta.value = txta.value.substring(0, the_start) + tag_seltxt + txta.value.substring(the_start + tag_seltxt.length, txta.value.length);

    var pos = txta.value.length - (counter[tag] ? end.length : 0);    // Sets location for cursor position
    tr.collapse(true);
    tr.moveEnd('character', pos);        // start position
    tr.moveStart('character', pos);        // end position
    tr.select();                 // selects the zone
  }
  else if (txta.selectionStart || txta.selectionStart == "0") {
    var startPos = txta.selectionStart;
    var endPos = txta.selectionEnd;
    var tag_seltxt = counter[tag] ? txta.value.substring(startPos, endPos) + end : start + txta.value.substring(startPos, endPos);
    txta.value = txta.value.substring(0, startPos) + tag_seltxt + '\u200C' + txta.value.substring(endPos, txta.value.length);

    // Place the cursor between formats in #txta
    if(counter[tag])
      txta.setSelectionRange((endPos),(endPos));
    else
      txta.setSelectionRange((endPos+start.length+1),(endPos+start.length+1));
    
    txta.focus();
  }
  
  counter[tag]++;
  counter[tag]%=2;
  return tag_seltxt;
}
document.getElementById('big').onclick = function() {
  var tag_seltxt = addTagSel('big');
  return tag_seltxt;
}
document.getElementById('b').onclick = function() {
  var tag_seltxt = addTagSel('b');
  return tag_seltxt;
}
document.getElementById('i').onclick = function() {
  var tag_seltxt = addTagSel('i');
  return tag_seltxt;
}
document.getElementById('u').onclick = function() {
  var tag_seltxt = addTagSel('u');
  return tag_seltxt;
}
document.getElementById('del').onclick = function() {
  var tag_seltxt = addTagSel('del');
  return tag_seltxt;
};
&#13;
.edit_button {
    display: inline-block;
    color: black;
    padding: 5px;
    background: #CCCCCC;
    cursor: pointer;
}
textarea {
    width: 400px;
    height: 50px;
    display: block;
}
&#13;
<a class="edit_button" id="big">&nbsp;Title&nbsp;</a>
<a class="edit_button" id="b">&nbsp;B&nbsp;</a>
<a class="edit_button" id="i">&nbsp;<i>I</i>&nbsp;</a>
<a class="edit_button" id="u">&nbsp;<u>U</u>&nbsp;</a>
<a class="edit_button" id="del">&nbsp;<del>S</del>&nbsp;</a>


<textarea id="wmd-input"></textarea>
&#13;
&#13;
&#13;