![]() |
![]() ![]() |
bbCode toolbar dùng để nhập nhanh bbCode cho textarea, sử dụng tốt cho các diễn đàn hoặc blog. Code sử dụng tốt trên các trình duyệt cảm ứng hoặc web.
Bước 1: chèn javascript sau vào trước form:
<script type="text/javascript">
function tag(text1,text2){
if((document.selection)){
document.form.msg.focus();
document.form.document.selection.createRange().text = text1+document.form.document.selection.createRange().text+text2
}else if(document.forms['form'].elements['msg'].selectionStart!=undefined){
var element=document.forms['form'].elements['msg'];
var str=element.value;
var start=element.selectionStart;
var length=element.selectionEnd-element.selectionStart;
element.value=str.substr(0,start)+text1+str.substr(start,length)+text2+str.substr(start+length)
}else{
document.form.msg.value+=text1+text2
}
};
function show_hide(a){
b=document.getElementById(a);
if(b.style.display=="none"){
b.style.display="block"
}else{
b.style.display="none"
}
}
</script>
function tag(text1,text2){
if((document.selection)){
document.form.msg.focus();
document.form.document.selection.createRange().text = text1+document.form.document.selection.createRange().text+text2
}else if(document.forms['form'].elements['msg'].selectionStart!=undefined){
var element=document.forms['form'].elements['msg'];
var str=element.value;
var start=element.selectionStart;
var length=element.selectionEnd-element.selectionStart;
element.value=str.substr(0,start)+text1+str.substr(start,length)+text2+str.substr(start+length)
}else{
document.form.msg.value+=text1+text2
}
};
function show_hide(a){
b=document.getElementById(a);
if(b.style.display=="none"){
b.style.display="block"
}else{
b.style.display="none"
}
}
</script>
Bước 2:
- trong thẻ form thêm thuộc tính name="form"
- trong thẻ textarea, nếu thuộc tính name khác "msg" thì tìm trong đoạn JavaScript trên chỗ nào có msg thì thay hết thành name của textarea.
Cách sử dụng:
- Để thêm một nút bấm, chỉ cần thêm code theo mẫu sau:
<a href="javascript:tag('[b]', '[/b]')">In đậm</a>
- Thay [ b ] và [/b] thành thẻ bbCode theo ý muốn
Chúc các bạn thành công!
#1 (0) |