Java全栈学习:JavaScript---void运算符,控制语句

一.void运算符

这个运算符用在既保留了超链接,点击超链接时执行一段js代码,但页面不能跳转。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>Hello World</p>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <a href = "javascript:void(0)" onclick = "alert('111')">你好,世界</a>
</body>
</html>

注意,如果写成href = "",表示的是路径是一个空的字符串,而写成href = "void(0)",表示的路径是一个叫做void(0)的路径,只有表示成href = "javascript:void(0)",才说明路径为空,自然不会跳转(javascript说明后面是一段js代码)。

二.控制语句

有七个在Java学习当中介绍过,if, switch, while, do...while, for, break, continue
我们介绍两个新的
for in

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script type = "text/javascript">
        var arr = [11, "abc", false, true, 55];
        for(var i = 0;i < arr.length; i++){
     
     
            alert(arr[i]);
        }

        for(var j in arr){
     
     
            alert(arr[j]);
        }
    </script>
</body>
</html>

作用一:遍历数组,上例子中的j代表了in后面的数组arr的下标。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script type = "text/javascript">
       User = function(username, password){
     
     
           this.username = username;
           this.password = password;
       }
       var u = new User("张三", "1111");
       alert("1" + u.username + u.password);
       alert("2" + u["username"] + u["password"]);

       for(var shuXingMing in u){
     
     
           //alert(shuXingMing);//可以获得属性名username和password
           //alert(typeof shuXingMing);//String类型
           alert("3" + u[shuXingMing]);
       }
    </script>
</body>
</html>

作用二:遍历对象属性,上面例子中shuXingMing为变量名,是String类型的,所以本身有""号,不能写成u["shuXingMing"].



with
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script type = "text/javascript">
       User = function(username, password){
     
     
           this.username = username;
           this.password = password;
       }
       var u = new User("张三", "1111");

       with(u){
     
     
            alert(username + password);
       }
    </script>
</body>
</html>

用法如上例子。

猜你喜欢

转载自blog.csdn.net/weixin_45965358/article/details/114199307