javascript代码实例教程-jquery第二期:三个例子带你走进jquery

发布时间:2019-03-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-jquery第二期:三个例子带你走进jquery脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 jquery是完全支持css的,我们举个例子来看看使用jquery的方便之处,这功劳是属于选择器的:

 例1:

[htML]  

<!DOCTYPE html PubLIC "-//W3C//DTD @L_777_0@ 4.01 TransITional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

<html>  

<head>  

<;meta http-equiv="Content-type" content="text/html; charset=UTF-8">  

<title>Insert title here</title>  

<script type="text/javascript" src="jquery-1.10.1.js"></script>  

<script type="text/javascript">  

   $(function()  

   {  

      $("li.abc").css("color","red");  

       });  

</script>  

</head>  

<body>  

<p id="hello">  

  <ul>  

    <li>niujiabin</li>  

    <li class="abc">maybe</li>  

    <li>gossipgo</li>  

  </ul>     

</p>  

  

</body>  

</html>  

我们想要做到改变maybe字体颜色红色

[html] 

$("li.abc")  

利用选择器可以直接获取到,非常方便,如果利用javascript获取就很麻烦

 

[html]  

.css("color","red");  

<li>之间的文本就会变成红色

 

运行结果:

 

例2:

[html] 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

<html>  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  

<title>Insert title here</title>  

<script type="text/javascript" src="jquery-1.10.1.js"></script>  

<script type="text/javascript">  

   $(function()  

   {  

      $("#hello ul li:even").css("background","grey").css("color","red");  

       });  

</script>  

</head>  

<body>  

<p id="hello">  

  <ul>  

    <li>niujiabin</li>  

    <li class="abc">maybe</li>  

    <li>gossipgo</li>  

  </ul>     

</p>  

  

</body>  

</html>  

[html]  

$("#hello ul li:even")  

选取id为hello下的ul下的li的奇数行。

[html] 

<span style="font-Size:18px">.css("color","red");</span>  

改变奇数行的颜色

 

运行结果:

 

 

 

例3:

 

[html]  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

<html>  

<head>  

  

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  

<title>Insert title here</title>  

<style type="text/css">  

.bg{  

background:#f00;  

color:#fff;  

}  

</style>  

<script type="text/javascript" src="jquery-1.10.1.js"></script>  

<script type="text/javascript">  

   $(function()  

   {  

     $("li").mouseover(setColor).mouseout(setColor);  

  

     function setColor()  

     {  

         $(this).toggleClass("bg");  

     }  

       });  

</script>  

</head>  

<body>  

<p id="hello">  

  <ul>  

    <li>niujiabin</li>  

    <li class="abc">maybe</li>  

    <li>gossipgo</li>  

  </ul>     

</p>  

  

</body>  

</html>  

 

[html]  

<style type="text/css">  

.bg{  

background:#f00;  

color:#fff;  

}  

</style>  

css样式,设置背景色和颜色

 

$("li").mouseover(setColor).mouseout(setColor);

为li添加事件

 

运行结果是鼠标放到哪就会显示变化,离开就会消失

 

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-jquery第二期:三个例子带你走进jquery全部内容,希望文章能够帮你解决javascript代码实例教程-jquery第二期:三个例子带你走进jquery所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。