javascript 在html中的四种使用方法和菱形小程序

javascript 在html中的四种使用方法

1.1行内式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- c+tab或者Ctrl+/注释 -->
<!--第一种 行内式,事件方式 onclick鼠标单击  ondblclick双击-->
    <button onclick="document.body.style.backgroundColor = this.innerText">red</button>
    <button onclick="document.body.style.backgroundColor = this.innerText">yellow</button>
    <button onclick="document.body.style.backgroundColor = this.innerText">blue</button>
<!--代码严格区分大小写,内容不区分-->
    <button onclick="document.body.style.backgroundColor = this.innerText">Orange</button>
    <button onclick="document.body.style.backgroundColor = this.innerText">#ccc</button>
    <button ondblclick="document.body.style.backgroundColor = this.innerText">#bbb</button>
<!--onmouseover 鼠标移动到按钮上-->
    <button onmouseover="document.body.style.backgroundColor =`rgb(${Math.round(Math.random()*255)},
    ${Math.round(Math.random()*255)},${Math.round(Math.random()*255)})`">random-color</button>
</body>
</html>

1.2嵌入式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2 id="hh">hello 中文</h2>
    <h2 >无id</h2>


<!--第二种script标签直接写代码    -->
    <script>
        let h2 = document.getElementById('hh')
        h2.style.color = 'red'
    </script>
</body>
</html>

1.3外链式(使用最多)

image-20220517213152163

image-20220517213213074

1.4直接运行

必须提前配好node

因为直接运行是在node中运行,所以只能运行简单,不能运行样式效果(样式效果在浏览器上运行显示)

image-20220517213737059

image-20220517214123152

1.5菱形小程序

1.1

for ( i=1;i<=25;i+=2){
    console.log('*'.repeat(i))
}

image-20220517214416513

1.2

let sp = 30
for ( i=1;i<=25;i+=2){
    console.log(' '.repeat(--sp)+'*'.repeat(i))
}

image-20220517214905969

1.3

let sp = 30
for ( i=1;i<=25;i+=2){
    console.log(' '.repeat(--sp)+'*'.repeat(i))
}
for ( i=23;i>=1;i-=2){
    console.log(' '.repeat(++sp)+'*'.repeat(i))
}

image-20220517215241031

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值