search
HomeWeb Front-endJS TutorialjQuery User Manual 3 CSS Operation_jquery

传统javascript对css的操作相当繁琐,比如

css
取它的background语法是 document.getElementById("a").style.background,而jQuery对css更方便的操作,$("#a").background(),$("#a").background(“red”)
$("#a")得到jQuery对象[
]
$("#a").background()将取出该对象的background样式。
$("#a").background(“red”)将该对象的background样式设为redjQuery提供了以下方法,来操作css
background ()   background (val)     color()    color(val)     css(name)    css(prop)   
css(key, value)      float()   float(val)   height()   height(val)  width()  width(val) 
left()   left(val)       overflow()   overflow(val)   position()   position(val)  top()   top(val)


这里需要讲解一下css(name)  css(prop)  css(key, value),其他的看名字都知道什么作用了!
div id="a" style="background:blue; color:red">cssdiv>id="b">testP>

css(name)  获取样式名为name的样式
$("#a").css("color") 将得到样式中color值red,("#a").css("background ")将得到blue

css(prop)  prop是一个hash对象,用于设置大量的css样式
$("#b").css({ color: "red", background: "blue" });
最终效果是

test

,{ color: "red", background: "blue" },hash对象,color为key,"red"为value,

css(key, value)  用于设置一个单独得css样式
$("#b").css("color","red");最终效果是

test



                                              :JavaScript处理

$.browser()  判断浏览器类型,返回boolen值
$(function(){
    
if($.browser.msie) {
        alert(
"这是一个IE浏览器");}
    
else if($.browser.opera) {
        alert(
"这是一个opera浏览器");}
})
当页面载入式判断浏览器类型,可判断的类型有msie、mozilla、opera、safari

$.each(obj, fn)  obj为对象或数组,fn为在obj上依次执行的函数,注意区分$().each()
$.each( [0,1,2], function(i){ alert( "Item #" + i + "" + this ); });
    分别将0,1,2为参数,传入到function(i)中
$.each({ name: "John", lang: "JS" },  function(i){ alert( "Name: " + i + ", Value: " + this );
    { name: "John", lang: "JS" }为一个hash对象,依次将hash中每组对象传入到函数中

$.extend(obj, prop)  用第二个对象扩展第一个对象
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
执行后settings对象为{ validate: true, limit: 5, name: "bar" }
可以用下面函数来测试
$(function(){
       
var settings = { validate: false, limit: 5, name: "foo" };
        
var options = { validate: true, name: "bar" };
        $.extend(settings, options);
        $.each(settings,  
function(i){ alert( i + "=" + this ); });
})

$.grep(array,fn)  通过函数fn来过滤array,将array中的元素依次传给fn,fn必须返回一个boolen,如fn返回true,将被过滤
$(function(){
        
var arr= $.grep( [0,1,2,3,4], function(i){ return i > 2; });
        $.each(arr, 
function(i){ alert(i); });
})
我们可以看待执行$.grep后数组[0,1,2,3,4]变成[0,1]

$.merge(first, second)  两个参数都是数组,排出第二个数组中与第一个相同的,再将两个数组合并
$(function(){ 
        
var arr = $.merge( [0,1,2], [2,3,4] )
        $.each(arr,  
function(i){ alert(i); });
})
可以看出arr的结果为[0,1,2,3,4]

$.trim(str)  移出字符串两端的空格
    $.trim("   hello, how are you?   ")的结果是"hello, how are you?"

                                   :动态效果

       在将这部分之前我们先看个例子,相信做网页的朋友都遇到n级菜单的情景,但点击某菜单按钮时,如果它的子菜单是显示的,则隐藏子菜单,如果子菜单隐藏,则显示出来,传统的javascript做法是先用getElementById取出子菜单所在容器的id,在判断该容器的style.display是否等于none,如果等于则设为block,如果不等于这设为none,如果在将效果设置复杂一点,当点击按钮时,不是忽然隐藏和显示子菜单,而是高度平滑的转变,这时就要通过setTimeout来设置子菜单的height了,再复杂一点透明度也平滑的消失和显现,这时显现下来需要编写很多代码,如果js基础不好的朋友可能只能从别人写好的代码拿过来修改了!jQuery实现上面效果只需要1句话就行,$("#a").toggle("slow"),,学完jQuery后还需要抄袭修改别人的代码吗?下面我们逐个介绍jQuery用于效果处理的方法。

hide()  隐藏匹配对象
id="a">Hello Againp>href="#" onClick=' ("#a").hide()'>jQuerya>
当点击连接时,id为a的对象的display变为none。

show() 显示匹配对象

hide(speed)  以一定的速度隐藏匹配对象,其大小(长宽)和透明度都逐渐变化到0,speed有3级("slow", "normal",  "fast"),也可以是自定义的速度。

show(speed)  以一定的速度显示匹配对象,其大小(长宽)和透明度都由0逐渐变化到正常

hide(speed, callback)  show(speed, callback) 当显示和隐藏变化结束后执行函数callback

toggle()    toggle(speed) 如果当前匹配对象隐藏,则显示他们,如果当前是显示的,就隐藏,toggle(speed),其大小(长宽)和透明度都随之逐渐变化。
jQuery User Manual 3 CSS Operation_jqueryimg src="1.jpg" style="width:150px"/>
jQuery User Manual 3 CSS Operation_jquery
href="#" onClick='$("img").toggle("slow")'>jQuerya>

fadeIn(speeds)   fadeOut(speeds)  根据速度调整透明度来显示或隐藏匹配对象,注意有别于hide(speed)和show(speed),fadeIn和fadeOut都只调整透明度,不调整大小
img src="1.jpg" style="display:none"/>href="#" onClick='$("img ").fadeIn("slow")'> jQuery a>
After clicking the connection, you can see the picture gradually display.

fadeIn(speed, callback) fadeOut(speed, callback) callback is a function, first display or hide the matching object by adjusting the transparency, and execute the callback function after the adjustment is completed
img src="1.jpg"/>
a href="#" onClick='$ ("img ").fadeIn("slow",function(){ alert("Animation Done."); })'> jQuery a>
After clicking the connection, you can see the picture gradually displaying, and a dialog box will pop up after the display is complete

fadeTo(speed, opacity, callback) Adjust the opacity of the matching object at speed, and then execute the function callback. Opacity is the final transparency (0-1).
img src="1.jpg"/>br>
a href="# " onClick='$("img ").fadeTo("slow",0.55,function(){ alert("Animation Done."); })'> jQuery a>
You can Take a look and see the effect yourself. If you don't use jQuery, writing original javascript scripts may be a lot of code!

slideDown(speeds) Smoothly change the height of the matching object from 0 to normal at the specified rate!
img src="1.jpg" style="display :none"/>
a href= "#" onClick='$("img ").slideDown("slow")'>jQuerya>

slideDown(speeds,callback) Change the height of the matching object from 0 to normal! After the change is completed, execute the function callback

slideUp("slow") slideUp(speed, callback) The height of the matching object changes from normal to 0

slideToggle("slow") If the height of the matching object is normal, it will gradually change to 0. If it is 0, it will gradually change to normal
Statement
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
JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

See all articles

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment