دستيابي به رشته انتخاب شده در TExtBox
سلام
توي برنامه من يه Textboxهست و يك دكمه اي به نام B . من ميخوام وقتي كه كاربر متن مورد نظرش رو توي اين textboxوارد كرد و بخشي از متن رو انتخاب كرد با زدن دكمه B دو طرف متن انتخاب شده دو تگ
<b/> و <b> قرار بگيرند.مثلا: اگر كاربر كلمه "سلام خوبي"رو وارد كرد و بعد كلمه "خوبي" رو انتخاب كرد و دكمه B رو زد اين كلمه توي textbox با اين صورت ظاهر بشه
سلام <b/> خوبي <b>.
تو رو خدا كمكم كنيد خيلي فوریه در ضمن نمي خوام از editor استفاده كنم
نقل قول: دستيابي به رشته انتخاب شده در TExtBox
کد HTML:
<script language=javascript>
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
document.aform.selectedtext.value = txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>
نقل قول: دستيابي به رشته انتخاب شده در TExtBox
نقل قول:
نوشته شده توسط
ali.akhbary
کد HTML:
<script language=javascript>
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
document.aform.selectedtext.value = txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform >
<textarea name="selectedtext" rows="5" cols="20"></textarea>
</form>
این کد برای نمایش متن انتخاب شده در کل صفحه هستش
با این کد اگر کاربر یه متن بیرون textbox رو هم انتخاب کنه متن نمایش داده می شه
نقل قول: دستيابي به رشته انتخاب شده در TExtBox
سلام.
این یک نمونه:
http://stackoverflow.com/questions/2...ith-javascript
function ShowSelection()
{
var textComponent = document.getElementById('Editor');
var selectedText;
// IE version
if (document.selection != undefined)
{
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
// Mozilla version
else if (textComponent.selectionStart != undefined)
{
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos)
}
alert("You selected: " + selectedText);
}
document.onkeydown = function (e) { ShowSelection(); }
این هم یک منبع خوب برای یادگیری:
http://www.quirksmode.org/dom/range_intro.html
موفق باشید.
نقل قول: دستيابي به رشته انتخاب شده در TExtBox
از همتون ممنونم كه به سؤالم پاسخ داديد