JQuery learning attributes and styles of notes

.attr () and .removeAttr ()

attr () There are four expressions

  • attr (incoming property name): Gets the property value
  • attr (attribute name, attribute value): set the value of the property
  • attr (attribute name, function value): value of the property set function
  • attr (attributes): set to the plurality of specified element property values, namely: {a property name: "an attribute value" name two attributes: "attribute value II", ...}

html (), text () and val ()

.html () ,. differences in text () and .val () summary:

  • .html () ,. text () ,. val () three methods are used to read the contents of the selected element; only:
    • .html () is used to read the contents of html elements (including html tags)
    • .text () is used to read plain text elements, including elements progeny
    • .val () is used to read the form elements "value" value.
  • .html () and .text () method can not be used on the form element, .val () can only be used on the form element; Further .html () method on a plurality of elements, a first read only elements; .val () method and .html () the same, if applied in a plurality of elements which can be read from the first form element "value" value, but .text () and they are not the same, if .text () when applied to multiple elements, will read the text content of all selected elements.
  • .html (htmlString) ,. three methods of text (textString) and .val (value) are used to replace the contents of the selected element, if the three methods used in the multiple elements simultaneously, it will replace all selected elements Content.
  • .html () ,. text () ,. val () can use the content of the callback function to dynamically change the values ​​of a plurality of elements.

addClass(), removeClass()和toggleClass()

  • addClass (name): can be added to an element of a class (class);
  • removeClass (name): it is clear that the deletion of a class to an element name
  • toggleClass (name): switching the class name

css()

css () Gets the style elements can, of course, can set style:

  • .css (propertyName, value): Set CSS
  • .css (propertyName, function): a callback function can be passed, returns to get the corresponding values ​​are processed
  • .css (properties): an object can pass, while a plurality of styles, incoming properties need to be a dict (json format)

example:


$('p').css('color') //获取p的颜色属性
$('p').css(['color', 'width']) //获取p的颜色属性和宽度
$('p').css('color', 'red') //设置p的颜色为red
$('p').css({'color': 'red', 'width':'100px'}) //设置p的颜色和width


MARSGGBO Original





2019-8-13



Guess you like

Origin www.cnblogs.com/marsggbo/p/11345151.html