您的位置:首页 > Web前端 > JQuery

jQuery基本整理2

2018-01-16 10:45 141 查看

jQuery基本整理<2>

@[基本实例|基于bootstrap框架]

jQuery基本整理2

jQuery HTML
jQuery获取

jQuery设置

jQuery添加元素

jQuery删除新元素

jQuery CSS

jQuery HTML

jQuery获取

三个简单实用的用于 DOM 操作的 jQuery 方法:

- text() - 设置或返回所选元素的文本内容

- html() - 设置或返回所选元素的内容(包括 - HTML 标记)

- val() - 设置或返回表单字段的值

$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
$("#btn1").click(function(){
alert("Value: " + $("#test").val());
});


jQuery attr() 方法用于获取属性值。

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


jQuery设置

我们将使用前一章中的三个相同的方法来设置内容:

- text() - 设置或返回所选元素的文本内容

- html() - 设置或返回所选元素的内容(包括 HTML 标记)

- val() - 设置或返回表单字段的值

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


jQuery attr() 方法也用于设置/改变属性值。

$("button").click(function(){
$("#w3s").attr("href","http://www.w3school.com.cn/jquery");
});


jQuery添加元素

jQuery append() 方法在被选元素的结尾插入内容

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


jQuery prepend() 方法在被选元素的开头插入内容

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


jQuery after() 方法在被选元素之后插入内容。

jQuery before() 方法在被选元素之前插入内容。

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

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


jQuery删除新元素

jQuery remove() 方法删除被选元素及其子元素

$("#div1").remove();


jQuery empty() 方法删除被选元素的子元素

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


jQuery CSS

addClass() - 向被选元素添加一个或多个类

$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});


removeClass() - 从被选元素删除一个或多个类

$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});


toggleClass() - 对被选元素进行添加/删除类的切换操作

$("button").click(function(){
$("h1,h2,p").toggleClass("blue");
});


css() - 设置或返回样式属性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jQuery