JS some tips 3

Source: Internet
Author: User

JS some tips

1. (1) Submit this form simultaneously by Ctrl+enter


Increasing onkeydown events in multiline text fields

<form id="Formnews_reply" class="Bbs-send-form form"  onsubmit="return news.add_newsreply (this);" >    <fieldset class="Inputs">        <label for ="Comments">Comments</label>        <textarea rows="Ten" 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]"). GE        T (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) {    if13 && event.ctrlKey) {        var $thisForm=com.whuang.hsj.getForm(formElement);        var submitBtn=$thisForm.find(":button:first");        submitBtn.get(0).click();    }};

JS method Execution Process: First through the Com.whuang.hsj.getForm method to get the form of textarea, and then get the first button under the form, and finally trigger the button's OnClick event.
Note: ": Button:first" means the first button, similarly ": Input:first" means the first form control (input box, button, check box, etc.)
**
3. (2) achieve gradient return to top

**

The HTML code for "back to title" is as follows:

<a style="float: right;" class="aHref" onclick="anchorGoWhere(‘title_loc_bbs‘)">回到标题</a>

Anchorgowhere is the JS method, implemented as follows:

varfunction (anchorName) {$("#subPageBBS .subContent").animate({scrollTop: $("a[name=""]"1000);};

Description: $ ("#subPageBBS. Subcontent") is the div where the scroll bar is located
**

1. (3) Get native DOM elements from a jquery object

**
jquery object. Get (0). For example

$navHrefComment.get(0function () {        bbs.addBBSComment(cardid2);    };

(4) TextArea character length limit

/ * 1. TextArea character length limit 2. */Ismaxlen = function (event) {var obj = Event. srcelement? event. srcelement: Event. Target;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) Enter +ctrl to achieve line break

/ * * Enter +ctrl line * */newline = function (Event) {if(Event. keycode = = -&&Event. Ctrlkey) {if(document.selection) {varSelectText = Document.selection.createRange ();if(SelectText) {if(SelectText.text.length >0) Selecttext.text + ="\ r \ n";ElseSelecttext.text ="\ r \ n"; SelectText.Select(); }         }Else{varobj =Event. srcelement?Event. srcelement:Event. Target; Obj.value+="\ r \ n"; }         }     };

Reference:
JS some tips
JS some tips 2

JS some tips 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.