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

jQuery basic syntax summary

怪我咯
Release: 2017-06-27 11:13:36
Original
1695 people have browsed it

jQuery syntax:

jQuery syntax is compiled for the selection of HTML elements and can perform certain operations on the elements. This is the key point. Beginners must know what the things you learn are used for.

The basic syntax is: $(selector).action(). All jQuery revolves around this, selecting elements on the page and then performing certain operations on the elements.

Example

$(this).hide() - Hide the current element

Document ready function:

is to prevent jQuery code from running before the document is fully loaded (ready). Due to jQuery's convention, all JavaScript code is best placed here.


$(document).ready(function(){
});
Copy after login





##jQuery Element selectors and attribute selectors: They allow you to select HTML elements by tag name, attribute name or content. Corresponds to the first half of $(selector).action().

jQuery element selector:

$("p") Selects the

element.

$("p.intro") Selects all

elements with class="intro".

$("p#demo") Selects all

elements with id="demo".

jQuery attribute selector:


$("[href]") Select all items with href attribute of the element.

$("[href='#']") Selects all elements with an href value equal to "#".

$("[href!='#']") Selects all elements with an href value not equal to "#".


$("[href$='.jpg']") Selects all elements whose href value ends with ".jpg".

jQuery CSS selector:

$("p").css("background-color","red");

For the complete jquery selector reference manual: http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp





jQuery event function: corresponds to the second half of $(selector).action().


Event handlers refer to methods that are called when certain events occur in HTML.



<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>

</html>
Copy after login


##Event function$(document).ready(function)$(selector).click(function) $(selector).dblclick(function)$(selector).focus(function)$(selector).mouseover(function)

完整的jQuery事件:http://www.w3school.com.cn/jquery/jquery_ref_events.asp




约定:


  • 把所有 jQuery 代码置于事件处理函数中

  • 把所有事件处理函数置于文档就绪事件处理器中

  • 把 jQuery 代码置于单独的 .js 文件中

  • 如果存在名称冲突,则重命名 jQuery 库





jQuery 效果对应$(selector).action()的后半部分。

隐藏、显示、切换,滑动,淡入淡出,以及动画

通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素

$(selector).hide(speed,callback);

$(selector).show(speed,callback);


可选的 speed 参数规定隐藏/显示的速度,可以取以下值:"slow"、"fast" 或毫秒。

可选的 callback 参数是隐藏或显示完成后所执行的函数名称。











这是一个段落。


这是另一个段落。


可以使用 toggle() 方法来切换 hide() 和 show() 方法。

$(selector).toggle(speed,callback);










这是一个段落。


这是另一个段落。







jQuery 淡入淡出方法

通过 jQuery,您可以实现元素的淡入淡出效果。

jQuery 拥有下面四种 fade 方法:

  • fadeIn()

  • fadeOut()

  • fadeToggle() jQuery fadeToggle() 方法可以在 fadeIn() 与 fadeOut() 方法之间进行切换。

  • fadeTo() fadeTo() 方法允许渐变的最终结果为给定的不透明度(值介于 0 与 1 之间,0为透明,1为不透明)。





jQuery 滑动方法


通过 jQuery,您可以在元素上创建滑动效果。

jQuery 拥有以下滑动方法:

  • slideDown()

  • slideUp()

  • slideToggle()





jQuery 动画 - animate() 方法


jQuery animate() 方法用于创建自定义动画。


$(selector).animate({params},speed,callback);
Copy after login


必需的 params 参数定义形成动画的 CSS 属性。

可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。


可选的 callback 参数是动画完成后所执行的函数名称。

如需对位置进行操作,要记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute!

You can use the animate() method to operate all CSS properties. One important thing to remember: when using animate(), you must use Camel notation to write all property names, for example, You must use paddingLeft instead of padding-left, marginRight instead of margin-right, and so on.


It moves the

element to the left until the left attribute is equal to 250 pixels:





< ;body>


< ;/p>

jQuery provides queue functionality for animations, which means that if you write multiple animate() calls, jQuery creates an "internal" queue containing these method calls. Then run these animate calls one by one.

$("button").click(function(){
var p=$("p");
p.animate({height:'300px',opacity:'0.4 '},"slow");
p.animate({width:'300px',opacity:'0.8'},"slow");
p.animate({height:'100px',opacity: '0.4'},"slow");
p.animate({width:'100px',opacity:'0.8'},"slow");

});

You can use the following format instead

p.animate({height:'300px',opacity:'0.4'},"slow").animate({width:'300px',opacity:'0.8 '},"slow");




##jQuery stop() method is used to stop animations or effects before they are completed. Make a stop.

$(selector).stop(stopAll,goToEnd);

The optional stopAll parameter specifies whether the animation queue should be cleared. The default is false, which only stops active animations, allowing any queued animations to execute backwards.

The optional goToEnd parameter specifies whether to complete the current animation immediately. The default is false.


##



<script><br>$(document).ready(function(){<br> $("#flip").click(function( ){<br> $("#panel").slideDown(5000);<br> });<br> $("#stop").click(function(){<br> <br> $(" #panel").stop();<br><strong> });</strong>});<br></script>







Click here to slide the panel down< /p>

Hello world!








The Callback function is executed after the current animation is 100% completed.

$(selector).hide(speed,callback)

$("p").hide(1000,function(){

alert("The paragraph is now hidden" );


});

Full animation: http://www.w3school.com.cn/jquery/jquery_ref_effects.asp




jQuery has powerful methods for manipulating HTML elements and attributes.

jQuery DOM operation:

DOM = Document Object Model, "The W3C Document Object Model is a platform- and language-independent interface that allows programs and scripts to dynamically access and update documents. Content, structure and style.”


##Get the value in dom:

三A simple and practical jQuery method for DOM manipulation:

text() - Set or return the text content of the selected element

  • html () - Sets or returns the content of the selected element (including HTML tags)

  • val() - Sets or returns the value of the form field

  • $("#btn1 ").click(function(){
  • alert("Text: " + $("#test").text());

    });

    $("#btn2").click (function(){

    alert("HTML: " + $("#test").html());
    });

    $("#btn1").click(function(){
    alert("Value: " + $("#test").val());
    });

    $("button").click(function(){
    alert($("#w3s").attr("href"));
    });

Set DOM value:

$("#btn1").click(function(){
$("#test1") .text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");

});

The callback function of text(), html() and val(), The callback function consists of two parameters: the selected element The index of the current element in the list, and the original (old) value. Then the return content of the function return is used as the new value.

$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});

$("#btn2").click (function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText + " New html: Hello world!< /b>
(index: " + i + ")";
});

});

$("button").click(function() {
$("#w3s").attr("href", function(i,origValue){
return origValue + "/jquery";
});

}) ;




Add a new value to the DOM:


  • append() - Insert content at the end of the selected element

  • prepend() - Insert content at the beginning of the selected element

  • after() - Insert content after the selected element

  • before() - Insert content before the selected element


$("p").append("Some appended text.");

$("p").prepend("Some prepended text.");

$("img").after("Some text after");

$("img").before(" Some text before");




##Delete elements in the DOM:


If you need to delete elements and content, you can generally use the following two jQuery methods:

  • remove() - delete the selected element (and its children) Element)

  • empty() - Removes child elements from the selected element

  • ##$("#p1").remove();

$("#p1").empty();


jQuery remove() method also accepts a parameter, allowing you to delete the element Filter.

This parameter can be any jQuery selector syntax.

##

<script><br>$(document).ready(function(){<br> $("button").click(function(){<br> $("p" ).remove(".italic");<br> });<br>});<br></script>





This is a paragraph in the p.


This is another paragraph in the p.

This is another paragraph in the p.










##jQuery Manipulating CSS


jQuery has several methods for CSS manipulation. We will learn the following:


addClass() - Adds one or more classes to the selected element

Bind function to
Bind the function to the document’s ready event (when the document has finished loading)
Trigger or bind a function to the click event of the selected element
Trigger or bind a function to the double-click event of the selected element
Trigger or bind the function to the focus event of the selected element
Trigger or bind the function Mouseover event bound to the selected element