ช่วยด้วยครับ พี่ๆเก่งๆ javascript

เริ่มโดย JumDaiDee, 30 สิงหาคม 2014, 10:44:49

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

JumDaiDee

จาก code นี้  ต้องแก้โค้ดอย่างไรครับ
เมื่อคลิก btn แล้ว อยากให้ cursor default อยู่ตรงกลาง ระหว่างแท็กครับ
ตัวอย่าง ["b] แสดง sursor โดยไม่ต้องคลิกเองครับ [/b]

อ้างถึง<html>
<head>
<script type="text/javascript">
function addtag(tag) {
   var txt = document.getElementById('mta');
   if(document.selection) {
      txt.focus();
      sel = document.selection.createRange();
      sel.text = '['   tag   ']'   sel.text   '[/'   tag   ']';
   } else if(txt.selectionStart || txt.selectionStart == '0') {   
      txt.value = (txt.value).substring(0, txt.selectionStart)   "[" tag "]"   (txt.value).substring(txt.selectionStart, txt.selectionEnd)   "[/" tag "]"   (txt.value).substring(txt.selectionEnd, txt.textLength);
   } else {
      txt.value = '['   tag   '][/'   tag   ']';
   }
   return;
}
function addurltag() {
   var txt = document.getElementById('mta');
   var link = prompt("Type the address:", "http://");
   if(link.length == 0 || link == "http://") {
      return;
   } else {
      var link = "="   link;
      var text;
      var sel2 = "";
      if(document.selection) {
         txt.focus();
         sel = document.selection.createRange();
         sel2 = sel.text;
      } else if(txt.selectionStart || txt.selectionStart == '0') {
         sel2 = (txt.value).substring(txt.selectionStart, txt.selectionEnd);
      }
      if(sel2.length > 0) {
         text = sel2;
      } else {
         text = prompt("Enter the link text:", "");
      }
   }
   if(document.selection) {
      txt.focus();
      sel = document.selection.createRange();
      sel.text = "[url"   link   "]"   text   "[/url]";
   } else {
      txt.value = (txt.value).substring(0, txt.selectionStart)   "[url"   link   "]"   text   "[/url]"   (txt.value).substring(txt.selectionEnd, txt.textLength);
   }
   return;
}
</script>
<style type="text/css">
.bbButton {
   height: 30px;
   font-family: serif;
   font-size: 16pt;
}
</style>
</head>
<body>
<input type="button" value="B" onclick="addtag('b')" class="bbButton" style="width:30px; font-weight:bold;" />
<input type="button" value="I" onclick="addtag('i')" class="bbButton" style="width:30px; font-style:italic;" />
<input type="button" value="U" onclick="addtag('u')" class="bbButton" style="width:30px; text-decoration:underline;" />
<input type="button" value="Link" onclick="addurltag()" class="bbButton" />
<input type="button" value="List" onclick="addtag('list')" class="bbButton" />
<input type="button" value="Code" onclick="addtag('code')" class="bbButton" /><br />
<br />
<textarea id="mta" name="mta" cols="40" rows="7"></textarea>
</body>
</html>

:wanwan017: :wanwan017:
สินค้าออนไลน์ https://www.shoppook.com/product | โปรแกรมออนไลน์ : https://appnon.com

scanfire

ลองไล่ใหม่อีกครับ ผมเอาไปลองเทส alert ค่าไม่ออกเลย

ลองเปลี่ยนเป็น event click เพื่อdebug ดูก่อนนะ

http://stackoverflow.com/questions/1064089/inserting-a-text-where-cursor-is-using-javascript-jquery

ขอโทษนะช่วยได้แค่นี้งานเยอะ
ทำเว็บไซต์ Magento, รับทำเว็บไซน์ขายของ ,รับตัดเว็บไซต์ responsive  ,รับทำแอพมือถือ ,สอนเขียนโปรแกรม, บริษัทรับทำเว็บไซต์
โทร : 0970011614, 063-187-5099
email : [email protected]

JumDaiDee

สินค้าออนไลน์ https://www.shoppook.com/product | โปรแกรมออนไลน์ : https://appnon.com

scanfire

ว่าแต่ทำไมไม่ใช้ editor ที่เค้าพัฒนาแล้วอะครับ สงสัย

ไปปิดงานมาพอดี เห็นได้แล้วยินดีด้วยนะครับ
ทำเว็บไซต์ Magento, รับทำเว็บไซน์ขายของ ,รับตัดเว็บไซต์ responsive  ,รับทำแอพมือถือ ,สอนเขียนโปรแกรม, บริษัทรับทำเว็บไซต์
โทร : 0970011614, 063-187-5099
email : [email protected]

JumDaiDee

อ้างถึงจาก: scanfire ใน 30 สิงหาคม 2014, 13:52:17
ว่าแต่ทำไมไม่ใช้ editor ที่เค้าพัฒนาแล้วอะครับ สงสัย

ไปปิดงานมาพอดี เห็นได้แล้วยินดีด้วยนะครับ

อ๋อ พอดี ผมจะเพิ่ม ปุ๋มพิเศษ ส่วนตัว ถ้าเข้าไปแก้ editor เค้าคงจะยากกว่า
ก็เลย ลองหาดูเล่นๆ ครับ จนเจอจนได้ ขอบคุณครับ
สินค้าออนไลน์ https://www.shoppook.com/product | โปรแกรมออนไลน์ : https://appnon.com