智能社html5视频,目前最新《智能社石川 Nodejs+WebSocket+Html5》

jQuery设置内容和属性

一、设置内容以及回调函数

方法

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

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

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

例子

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

$("#test1").text("Hello world!");

});//设置文本内容

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

$("#test2").html("Hello world!");

});//设置元素内容

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

$("#test3").val("Dolly Duck");

});//设置值

1

2

3

4

5

6

7

8

9

$("#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!

(index: " + i + ")";

});

});//返回元素内容

1

2

3

4

5

6

7

8

9

10

11

12

13

TIPS

回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。

二、设置属性以及回调函数

方法

attr() - 设置属性值

例子

$("button").click(function(){

$("#w3s").attr("href","http://www.w3school.com.cn/jquery");

});//设置单个属性

$("button").click(function(){

$("#w3s").attr({

"href" : "http://www.w3school.com.cn/jquery",

"title" : "W3School jQuery Tutorial"

});

});//设置多个属性

1

2

3

4

5

6

7

8

9

10

$("button").click(function(){

$("#w3s").attr("href", function(i,origValue){

return origValue + "/jquery";

});

});//回调函数

1

2

3

4

5

总代码

jq设置内容和属性

$(document).ready(function(){

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

$("#p1").text("hello world1!");

});

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

$("#p2").html("

hello world2!

");

});

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

$("#t1").val("hello world3!");

});

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

$("#p1").text(function(i,origText){

return "旧文本:"+origText+"新文本:这是新文本(index:"+i+")"

});

});

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

$("#p2").html(function(i,origText){

return "旧HTML:"+origText+"新HTML:这是新HTML(index:"+i+")"

});

});

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

$("#t1").val(function(i,origText){

return "旧值:"+origText+"新值:这是新值(index:"+i+")"

});

});

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

$("a").attr("href","https://tieba.baidu.com");

});

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

$("a").attr("href",function(i,origValue){return origValue + "/s?wd=1&rsv_spt=1&rsv_iqid=0xbc3b96600002e5f4&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=2&rsv_sug1=1&rsv_sug7=100&rsv_sug2=0&inputT=163&rsv_sug4=1686"});

});

});

这是文本

这是HTML

这是:


百度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值