Js Tips 3. js tips
Js tips
1. (1) Simultaneously press Ctrl + Enter to submit this form
Add an onkeydown event in the multi-line text field
<Form id = "formNews_reply" class = "bbs-send-form" onsubmit = "return news. add_newsReply (this ); "> <fieldset class =" inputs "> <label for =" comments "> comments </label> <textarea rows =" 10 "name =" comments "placeholder =" Your comment "onkeydown =" com. whuang. hsj. ctrlEnter2Submit (event, this); "required> </textarea> </fieldset> <fieldset class =" submit2 inputs "> <input type =" button "onclick =" news. add_newsReply (this); "class =" ui-button "style =" margin-top: 5px; "value =" send "> </fieldset> <input type =" hidden "name =" targetId "value =" $ {targetId} "> <script type =" text/javascript "> $ (function () {$ ("# formNews_reply textarea [name = comments]"). get (0 ). focus () ;}); </script> </form>
The implementation of com. whuang. hsj. ctrlEnter2Submit is as follows:
/*** * 1. @param event : textarea onkeydown event 2. @param formElement : textarea itself */com.whuang.hsj.ctrlEnter2Submit= function (event,formElement) { if(event.keyCode == 13 && event.ctrlKey) { var $thisForm=com.whuang.hsj.getForm(formElement); var submitBtn=$thisForm.find(":button:first"); submitBtn.get(0).click(); }};
Js method execution process: first use the com. whuang. hsj. getForm method to obtain the form where textarea is located, then obtain the first button under the form, and finally trigger the onclick event of the button.
Note: ": button: first" indicates the first button. Similarly, ": input: first" indicates the first form control (input box, button, check box, etc)
**
3. (2) Implement Gradient Back to Top
**
The html code for "back to title" is as follows:
<A style = "float: right;" class = "aHref" onclick = "anchorGoWhere ('title _ loc_bbs ')"> return to the title </a>
AnchorGoWhere is a js method and is implemented as follows:
var anchorGoWhere = function (anchorName) {$("#subPageBBS .subContent").animate({scrollTop: $("a[name=" + anchorName + "]").offset().top}, 1000);};
Note: $ ("# subPageBBS. subContent") is the div of the scroll bar.
**
1. (3) obtain the native dom element from the jquery object
**
Jquery object. get (0). Example
$navHrefComment.get(0).onclick = function () { bbs.addBBSComment(cardid2); };
(4) textarea character length limit
/* 1. textarea character length limit 2. */isMaxLen = function (event) {var obj = event. srcElement? Event. srcElement: event.tar get; var target = $ ("#" + obj. getAttribute ("id "). replace (". ","\\. "); var maxLength = target. attr ("maxlength"); if (target. val (). length> maxLength) {target. blur (); target. val (target. val (). substring (0, maxLength); target. focus ();}};
(5) Press ENTER + CTRL to implement line feed
/** Press ENTER + CTRL to wrap the line **/newline = function (event) {if (event. keyCode = 13 & event. ctrlKey) {if (document. selection) {var selectText = document. selection. createRange (); if (selectText) {if (selectText. text. length> 0) selectText. text + = "\ r \ n"; else selectText. text = "\ r \ n"; selectText. select () ;}} else {var obj = event. srcElement? Event. srcElement: event.tar get; obj. value + = "\ r \ n ";}}};
Refer:
Js tips
Js Tips 2