JS操作元素的内容

对象.innerText 属性

对象.innerHTML 属性

<body>
    <div class='box'>文字</div>
    <script>
        //首先获取元素
        const box = document.querySelector('.box')
        console.log(box.innerText)
    </script>
</body>

1.元素innerText属性

将文本内容添加到标签任意位置

显示为纯文本,不解析标签

2.innetHTML解析标签

<body>
    <div class="box">
                
    </div>
    <script>
        const box = document.querySelector('.box')
        box.innerHTML =`<u>小狗</u>`
    </script>
</body>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>年会抽奖</title>
  <style>
    .wrapper {
      width: 840px;
      height: 420px;
      background-color: pink;
      padding: 100px 250px;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <strong>抽奖</strong>
    <h1>一等奖:<span id="one">???</span></h1>
    <h3>二等奖:<span id="two">???</span></h3>
    <h5>三等奖:<span id="three">???</span></h5>
  </div>
  <script>
    const arr = ['苹果', '香蕉', '橘子', '荔枝', '梨子', '小狗哦']
    const random = Math.floor(Math.random() * arr.length)
    console.log(arr[random])
    const one = document.querySelector('#one')
    one.innerHTML = arr[random]
    arr.splice(random, 1)

    const random2 = Math.floor(Math.random() * arr.length)
    const two = document.querySelector('#two')
    two.innerHTML = arr[random2]
    arr.splice(random2, 1)

    const random3 = Math.floor(Math.random() * arr.length)
    const three = document.querySelector('#three')
    three.innerHTML = arr[random3]

  </script>
</body>

</html>

抽奖案例,我尝试共用一个const random 这样会出现undefined情况。

这样分开写就不会出现冲突情况

操作元素常用属性:

       对象.属性=值

<body>
    <img src="./images/1.webp" alt="">
    <script>
        const a = document.querySelector('img')
        a.src = './images/2.webp'
    </script>
</body>

能够通过src实现换图操作

练习1:

<body>
    <img src="./images/1.webp" alt="">
    <script>
        function getrandom(n, m) {
            return Math.floor(Math.random() * (
                m - n + 1)) + n
        }

        const img = document.querySelector('img')
        const random = getrandom(1, 6)
        img.src = `./images/${random}.webp`
    </script>
</body>

操作元素样式属性

    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="box">

    </div>
    <script>
        const box = document.querySelector('.box')
        box.style.width = '300px'
        //单词可以直接用,组合单词采用小驼峰命名
        box.style.backgroundColor = 'hotpink'
    </script>
</body>

案例:

document.body.style

    <style>
        body {
            background: url(./images/desktop_1.jpg) no-repeat top center/cover;
        }
    </style>

<body>
    <script>
        function getrandom(n, m) {
            return Math.floor(Math.random() * m - n + 1) + n;
        }
        const a = getrandom(1, 10);
        console.log(a)
        document.body.style.background = `url(./images/desktop_${a}.jpg)`;
    </script>
</body>

body可以直接使用不需要获取。上述代码可实现刷新换背景图效果

2.操作元素样式属性(ClassName)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav{
            color: brown;
        }
        .box{
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <div class="nav">
        是小狗哦
    </div>
    <script>
        const a=document.querySelector('div')
        a.className='nav box'
    </script>
</body>
</html>

在这里如果使用ClassName方法需要把之前的加上,否则后面会覆盖前面的。

3.通过classList追加模式(新增)

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JS操作iframe里的元素可以通过以下几个步骤进行: 1. 获取iframe元素:首先,我们需要通过`document.getElementById()`或其他适合的方法获取到iframe元素的引用。例如,如果iframe元素的id为"myIframe",我们可以像这样获取它:`var iframe = document.getElementById("myIframe");` 2. 获取iframe的内容窗口:每个iframe都有一个`contentWindow`属性,它表示iframe的内容窗口。通过访问`iframe.contentWindow`,我们可以得到iframe内部的window对象引用。例如:`var iframeWindow = iframe.contentWindow;` 3. 获取iframe内部的文档对象:在获取到iframe内容窗口的引用后,我们可以通过`iframeWindow.document`来访问iframe内部的文档对象。例如:`var iframeDocument = iframeWindow.document;` 4. 使用iframe的文档对象进行操作:通过获取到iframe内部的文档对象,我们就可以像操作普通的HTML文档一样操作iframe中的元素了。例如,如果我们要获取iframe中id为"myElement"的元素,并改变它的样式,可以通过以下代码实现: ``` var myElement = iframeDocument.getElementById("myElement"); myElement.style.color = "red"; ``` 需要注意的是,由于同源策略的限制,当iframe与当前页面不属于同一个域时,我们只能通过以上方法获取到iframe元素本身,而无法直接访问其内部的文档对象。为了解决这个问题,可以考虑使用postMessage方法进行跨域通信,或者在iframe的源文件中添加合适的跨域资源共享(CORS)头部。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值