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

JQuery添加新元素

2015-11-24 19:50 861 查看
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73
<metahttp-equiv=
"Content-Type"
content=
"text/html;charset=utf-8"
>

<title>第一个JQuery文档</title>

<scriptsrc=
"jquery.js"
></script>

<scripttype=
"text/javascript"
>

//使用JQuery操纵网页元素,实现新元素添加并绑定事件。

$(document).ready(

function
(e){

$(
"#msg"
).css(
"font-size"
,
"9px"
);
//更改DIV元素的字体


$(
"#msg"
).click(

function
(e){

alert($(
this
).html());

}

);
//为DIV元素添加CLICK响应


$(
"<b>Hello
World!</b>"
).appendTo(
"body"
);
//向页面BODY添加一个新的B元素


$(
"<div>"
,{

style:
"width:200px;height:200px;background-color:silver;"

}).hover(
function
(){
alert(
'鼠标移入!'
);},

function
(){alert(
'鼠标移出!'
);
}

)

.appendTo(
"body"
);
//向页面BODY添加一个新的DIV元素,并添加JQuery的hover事件响应。


/*

*hover()是jQuery为了方便用户固定调用mouseenter和mouseleave事件而重新定义的内部事件,它并非一个真正的事件,不能使用bind()绑定。

*所以使用以下代码是无法实现Hover事件绑定效果的。

*/

$(
"<div>"
,{

style:
"width:200px;height:200px;background-color:purple;"

}).bind({hover:(
function
(){
alert(
'mouseover'
);
},

function
(){alert(
'mouseout'
);
}

)

}).appendTo(
"body"
);


/*

*可以直接绑定mouseenter/mouseleave或mouseover/mouseout事件达到与Hover事件相同效果。

*/

$(
"<div>"
,{style:
"width:200px;height:200px;background-color:maroon;"

}).bind({

mouseover:(
function
(){
alert(
'mouseover'
);
})

}).bind({

mouseout:(
function
(){
alert(
'mouseout'
);
})

}).appendTo(
"body"
);


$(
"<div>"
,{

style:
"font-size:9px;background-color:navy;width:200px;height:200px;"
,

text:
"单击这里改变颜色"
,

click:
function
(){

$(
this
).css(
"background-color"
,
"green"
);},

mouseenter:
function
(){

$(
this
).css(
"background-color"
,
"blue"
);},

mouseleave:
function
(){

$(
this
).css(
"background-color"
,
"yellow"
);}

}).appendTo(
"body"
);


$(
"<div>"
,{style:
"width:200px;height:200px;background-color:olive;"

}).bind({

click:(
function
(){
alert(
'mouseclick'
);
})

}).appendTo(
"body"
);
//向页面BODY添加一个新的DIV元素,并添加CLICK响应。

}

);

</script>



<divid=
"msg"
style=
"width:200px;height:200px;background-color:red"
><span>第一个JQuery文档</span></div>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: