Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the usage of css methods and event methods in jquery

伊谢尔伦
Release: 2017-06-19 14:46:00
Original
1238 people have browsed it

CSS method

.hasClass(calssName) checks whether the element contains a certain class and returns true/false

$( "#mydiv" ).hasClass( "foo" )
Copy after login

.addClass(className) / .addClass(function(index,currentClass)) Adding a class to an element does not overwrite the original class, it appends it, and it does not check for duplication

$( "p" ).addClass( "myClass yourClass" );
$( "ul li" ).addClass(function( index ) {  return "item-" + index;
});
Copy after login

removeClass([className]) / ,removeClass(function(index,class)) Remove element single/multiple/all class

$( "p" ).removeClass( "myClass yourClass" );
$( "li:last" ).removeClass(function() {  return $( this ).prev().attr( "class" );
});
Copy after login

.toggleClass(className) /.toggleClass(className,switch) / .toggleClass([switch]) / .toggleClass( function(index, class, switch) [, switch ] ) toggle means switching, the method is used for switching, switch is a bool type value, you can understand this by looking at the example

<div class="tumble">Some text.</div>
Copy after login

First execution

$( "div.tumble" ).toggleClass( "bounce" )
<div class="tumble bounce">Some text.</div>
Copy after login

Second execution

$( "div.tumble" ).toggleClass( "bounce" )
<div class="tumble">Some text.</div>
Copy after login
$( "#foo" ).toggleClass( className, addOrRemove );// 两种写法意思一样if ( addOrRemove ) {
  $( "#foo" ).addClass( className );
} else {
  $( "#foo" ).removeClass( className );
}
Copy after login
$( "div.foo" ).toggleClass(function() {  if ( $( this ).parent().is( ".bar" ) ) {    return "happy";
  } else {    return "sad";
  }
});
Copy after login

.css(propertyName) / .css(propertyNames) Get the value of a specific property of the element style

var color = $( this ).css( "background-color" ); 
var styleProps = $( this ).css([    "width", "height", "color", "background-color"
  ]);
Copy after login

.css(propertyName,value) / .css( propertyName, function(index, value) ) / .css( propertiesJson ) Set the value of a specific property of element style

$( "div.example" ).css( "width", function( index ) {  return index * 50;
});
$( this ).css( "width", "+=200" );
$( this ).css( "background-color", "yellow" );
   $( this ).css({      "background-color": "yellow",      "font-weight": "bolder"
    });
Copy after login

Event method

.bind( eventType [, eventData ], handler(eventObject) ) Bind DefinedEvent processing program, this is often used, no explanation required

$( "#foo" ).bind( "click", function() {
  alert( "User clicked on &#39;foo.&#39;" );
});
Copy after login

.delegate(selector, eventType, handler(eventObject)) Let’s see the official explanation

Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

$( "table" ).on( "click", "td", function() {//这样把td的click事件处理程序绑在table上
  $( this ).toggleClass( "chosen" );
});
Copy after login

.on( events [, selector ] [, data ], handler (eventObject) ) Recommended after 1.7, replacing bind, live, delegate

$( "#dataTable tbody" ).on( "click", "tr", function() {
  alert( $( this ).text() );
});
Copy after login

.trigger( eventType [, extraParameters ] ) JavaScriptStart element binding event

$( "#foo" ).trigger( "click" );
Copy after login

.toggle( [duration ] [, complete ] ) / .toggle( options ) Hide or show elements

$( ".target" ).toggle();
$( "#clickme" ).click(function() {
  $( "#book" ).toggle( "slow", function() {    // Animation complete.  });
});
Copy after login

The above is the detailed content of Detailed explanation of the usage of css methods and event methods in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!