2016 - 1 - 27 javaScrip初步(一)

本文介绍了如何使用JavaScript为网页元素添加点击事件,包括显示弹窗消息和控制台输出,同时演示了通过jQuery改变HTML元素的内容。此外还展示了如何定义JavaScript函数。
<head>
</head>

<body>
    <!-- The onclick attribute is the code       
             that happens when the element is clicked.       
             The value of the attribute is some      
             javascript code.        
             In this case it creates an alert        
             pop up dialog
    -->
    <h1 id="title" onclick="alert('hello');" >
            Hello
    </h1>
</body>

<head>
</head>
<body>
    <!--
        In this case the onclick functions
        writes something to the console.
        The console is an object so we use the
        dot (.) notation to call a function
        on it
    -->
    <h1 id="title"
        onclick="console.log('hello');" >
            Hello
    </h1>
</body>

  I don't know does the code " console.log('hello') " whether means make a object(console) do sth.?

 

 

2.How to use the jQuery

<head>
   <!-- to use jQuery we need to import it like this -->
   <script src=" http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
    <!--
        in this example use use jQuery to
            change the content itself.
            The $ is shorthand for the jQuery function
         We are passing in a CSS selector which
         gets this element by its id.
         The html function sets the html content
         of an element
    -->
    <h1 id="title" onclick="$('#title').html('Goodbye');">
        Hello
    </h1>
</body>

 

3.How to define a function in javaScript

<head>
       <script src = "http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head> 

<body>
    <h1 id="title" onclick="sayHello()"> Hello </h1>
</body>
<!--
     the script tag is where you can put
     more complex scripts
-->
<script type="text/javascript">
    
    function sayHello() {
        alert('Hello');
    };
</script>

 

转载于:https://www.cnblogs.com/BJTUzhengli/p/5166278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值