Use jquery to Remove extra characters and add ellipsis (custom jquery function)

Source: Internet
Author: User

<SCRIPT type = "text/JavaScript">
Jquery. FN. Limit = function (){
VaR self = $ ("[@ limit]");
Self. Each (function (){
VaR objstring = $ (this). Text ();
VaR objlength = $ (this). Text (). length;
VaR num = $ (this). ATTR ("Limit ");
If (objlength> num ){
$ (This). ATTR ("title", objstring );
Objstring = $ (this). Text (objstring. substring (0, num) + "...");
}
})
}
$ (Function (){
$ ("[@ Limit]"). Limit ();
})
</SCRIPT>

<Body>
<Div class = "blank"> press F5 to refresh the page .... </Div>
<Div id = "best">
<Div Limit = "12"> calculate the length and length of a string </div>
<Div Limit = "10"> optimizations are made public. </div>
<Div Limit = "12"> here we have optimized the public length and public length. </div>
<Div Limit = "12"> calculate the length of a word </div>
<Div Limit = "10"> There are optimizations, optimizations, and optimizations.

Very optimized </div>
</Div>
</Body>

 

PS: Writing of user-defined functions

1. Expand jquery's global functions

Jquery. logerror = {/* log error */}

Jquery. logwarning = {/* Log warning */}

Jquery. logdebug = {/* log debug */}

Use: jquery. logerror ();

The above three can be written

Jquery. log = {

Error: function () {/* log error */},

Warning: function () {/* Log warning */},

Debug: function () {/* log debug */}

}

Use: jquery. log. Error ();

Jquery. foobar = {/* Do foobar */}

Use: jquery. foobar ();

The above method is equivalent to using jquery. Extend ()

Jquery. Extend = {

Log: {error: function () {/* log error */},

Warning: function () {/* Log warning */},

Debug: function () {/* log debug */}

},

Foobar: function () {/* Do foobar */}

}

Usage:

Jquery. log. Error ();

Jquery. foobar ();

2. Add an instance method and extend jquery. fn.

Jquery. fn = jquery. Prototype

For example: jquery. FN. showmessage = function (){

Alert ("Message !!")}

Use: $ ("# div1"). showmessage ();

L this is a reference to the current jquery object.

L use each to iterate the current object

L The plug-in method must return a jquery object

 

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.