Home Web Front-end JS Tutorial jQuery intercepts string code of specified length_jquery

jQuery intercepts string code of specified length_jquery

May 16, 2016 pm 04:39 PM
intercept

Example, intercept string code.

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>jQuery截取字符串操作---www.jb51.net</title> 
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<style> 
* 
{ 
margin:0; 
padding:0; 
font-family:"宋体", Arial, Helvetica, sans-serif; 
} 
#best 
{ 
width:300px; 
height:200px; 
border:1px solid #ccc; 
margin:60px auto 0; 
line-height:1.6; 
font-size:14px; 
padding:10px 0 0 10px 
} 
.blank 
{ 
font-size:18px; 
font-weight:bold; 
text-align:center; 
padding:20px 
} 
</style> 
<script type="text/javascript"> 
jQuery.fn.limit=function(){ 
var self = $("div[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(){ 
$(document.body).limit(); 
}) 
</script> 
</head> 
<body> 
<div id="best"> 
<div limit="12">计算字串的长度长度长度长度</div> 
<div limit="10">这边有优化很公开这边</div> 
<div limit="12">这边有优化很公开长度长度很公开长度</div> 
<div limit="12">计算字长度长度</div> 
<div limit="10">这边有优化很边有优化很边有优化很边有优化很边有优化很</div> 
</div> 
</body> 
</html>
Copy after login

The above code implements the function of intercepting strings.

1. Implementation principle

Get the length of the text in the div, and then compare it with the length specified by the attribute limit. If it exceeds the length, use the interception specified length, and then replace it with....

2. Code comments

1.jQuery.fn.limit=function(){} is used to extend an instance function for jQuery. The jQuery object can call this function.
2.var self = $("div[limit]"), used to obtain a collection of div objects with limit attributes.
3.self.each(function(){ }, you can get each object in the div object collection to traverse and execute the specified function once.
4.var objString = $(this).text(), get the text content in the div element, where this refers to the current div when the each() function traverses.
5.var objLength = $(this).text().length, get the length of the text content in the current div.
6.var num = $(this).attr("limit"), get the limit attribute value in the div, which is used as the specified character length here.
7.if(objLength > num){}, the length of the text content in the div is greater than the specified length, this executes the specified code.
8.$(this).attr("title",objString), sets the value of the title attribute of the div to the content in the div.
9.objString = $(this).text(objString.substring(0,num) "..."), intercept the specified length string, and replace it with ellipses if it exceeds.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Detailed explanation of string interception method in Go language Detailed explanation of string interception method in Go language Mar 13, 2024 am 08:03 AM

Detailed explanation of string interception methods in Go language. In Go language, strings are immutable byte sequences, so some methods need to be used to implement string interception. String interception is a common operation to obtain a specific part of a string. You can intercept the first few characters, the last few characters of the string, or a certain length of characters from a specific position according to your needs. This article will introduce in detail how to intercept strings in Go language and provide specific code examples. Using slicing to implement string interception In Go language, you can use slicing to

How to use ROUND function to intercept decimal places in MySQL How to use ROUND function to intercept decimal places in MySQL Jul 13, 2023 pm 09:21 PM

How to use the ROUND function in MySQL to intercept the number of decimal places. In MySQL, you can use the ROUND function to intercept the number of decimal places. The ROUND function rounds a number to a specified number of decimal places. The following will introduce you to the use of ROUND function in detail and provide code examples. Syntax: ROUND(X,D)X represents the number to be rounded, and D represents the number of decimal places to be retained. Example of using the ROUND function to intercept the number of decimal places: Suppose there is a table named produc

PHP Chinese string interception skills: Say goodbye to mb_substr() PHP Chinese string interception skills: Say goodbye to mb_substr() Mar 15, 2024 pm 12:18 PM

In PHP development, we often encounter situations where Chinese strings need to be intercepted. Traditionally, we usually use the mb_substr() function to handle the interception of Chinese characters, but its performance is poor and not readable enough. This article will introduce some new Chinese string interception techniques, let us say goodbye to mb_substr(), and improve code efficiency and readability. Use regular expressions to intercept Chinese strings. Using regular expressions to intercept Chinese strings is an efficient and concise method. We can match Chinese characters through regular expressions

How UniApp implements image uploading and cropping How UniApp implements image uploading and cropping Jul 06, 2023 am 10:01 AM

UniApp is a cross-platform application development framework based on Vue.js, which can quickly develop applications for both iOS and Android platforms. In UniApp, uploading and cropping images is a common requirement. This article will introduce how to implement image uploading and cropping in UniApp, and provide corresponding code examples. 1. How to implement image upload: Use the uni.uploadFile() method to upload images. First, you need to configure uni.uploa

Quickly master the string interception function in Go language Quickly master the string interception function in Go language Mar 12, 2024 pm 06:15 PM

Quickly master the string interception function in Go language. Go language is a programming language that has attracted much attention in recent years. It has the characteristics of simplicity and efficiency, and is favored by more and more developers. In the Go language, string processing is a very common operation, and the string interception function is an important part of it. This article will use specific code examples to help you quickly master the string interception function in Go language. 1. Basic string interception. Strings in Go language can be intercepted through index. The code example is as follows: package

How to optimize string interception and splicing performance in Java development How to optimize string interception and splicing performance in Java development Jun 29, 2023 pm 06:04 PM

How to optimize the performance of string interception and splicing in Java development. In daily Java development, string interception and splicing are very common operations. However, due to the immutable nature of strings in Java, frequent string interception and splicing operations may cause performance degradation. In order to improve the performance of string interception and splicing in Java development, we can adopt the following optimization strategies. Concatenation using StringBuilder or StringBuffer: In Java, String

In-depth understanding of the implementation principle of string interception in Go language In-depth understanding of the implementation principle of string interception in Go language Mar 12, 2024 pm 06:27 PM

As a high-performance programming language, Go language provides a wealth of methods and functions for string operations and processing. Among them, string interception operation is one of the functions we often use. In this article, we will delve into the implementation principle of string interception in Go language and demonstrate the implementation process through specific code examples. What is string interception? In Go language, string interception refers to the operation of intercepting partial substrings from a string. By specifying the starting position and ending position, we can obtain the specified range in the original string

New features in Java 12: How to use the new String API to intercept and concatenate strings New features in Java 12: How to use the new String API to intercept and concatenate strings Jul 31, 2023 pm 12:55 PM

As a widely used programming language, Java has been continuously developed and updated. Each new version will introduce some new features and functions to improve developers' coding efficiency. One of the features of Java12 as the latest version is the introduction of the new StringAPI, which makes string interception and concatenation more convenient and efficient. In previous versions, string interception and concatenation operations needed to be implemented by calling substring() and the "+" operator, but these methods were unable to handle a large number of string operations.

See all articles