선택한 텍스트를 변수에 저장 한 다음 단추를 눌러 선택한 텍스트를 삭제하고 싶습니다. 가급적 jquery와 함께하지만 기본 자바 스크립트를 신경 쓰지 않는다.텍스트 영역에서 선택한 텍스트 얻기
미리 감사드립니다.
UPDATE :
감사 조지, 당신이 내가 필요로 코드를 제거 가리키는 예를 시도했지만 나는 그것이 버튼의 클릭에 동작하지 않습니다. #addchapter를 textarea로 변경 한 경우에만 작동합니다. 어떤 아이디어가 내 코드에 문제가 있습니까? 감사.
<html>
<head>
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery-fieldselection.js"></script>
<script type="text/javascript"><!--//--><![CDATA[//><!--
$(document).ready(function(){
$('#addchapter').click(update);
});
function update(e) {
var range = $(this).getSelection();
$('#output').html(
"selected text:\n<span class=\"txt\">" + ((true) ? range.text.whitespace() : range.text) + "</span>\n\n"
);
}
String.prototype.whitespace = (function() {
if (!RegExp.escape) {
RegExp.escape = (function() {
var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
var sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
return function(text) { return text.replace(sRE, '\\$1') }
})();
}
var ws = { "\r\n": "¶", "\n": "¶", "\r": "¶", "\t": "»", " ": "·" };
return ($.browser.msie) ? function() {
var s = this;
$.each(ws, function(i){ s = s.replace(new RegExp(RegExp.escape(i), 'g'), this) });
return s;
} : function() {
var s = this;
$.each(ws, function(i){ s = s.replace(new RegExp(RegExp.escape(i), 'g'), this + "\u200b") });
return s;
}
})();
//--><!]]>
</script>
</head>
<body>
<pre id="output"></pre>
<textarea id="area1" name="area1">textarea: foo bar baz</textarea>
<input type="button" value="add" id="addchapter">
</body>
</html>
업데이트 2 :
이 사용하여 종료 - http://plugins.jquery.com/project/a-tools 나는이 질문은 당신이 찾고있는 무엇을 가지고 있다고 생각합니다
그냥 교육을 추측하지만 스크립트 블록의 끝 부분에 짝수 처리기를 연결해야한다고 생각합니다. IIRC에서 JavaScript는 외부 파일의 코드를 포함 할 때 전체 파일을 읽지 않고 해당 블록에서 읽혀지는대로 평가됩니다. 즉, $ (document) .ready (function() { $ ('# addchapter') 클릭 (업데이트); }); '스크립트 블록의 끝으로 이동하십시오. –
안녕하세요, 그냥 시도했지만 작동하지 않는 것 같습니다. – usertest