js操作dom元素

1 创建元素 

// 例如创建iframe标签
const iframe = document.createElement('iframe');
//创建img
 const img = new Image();
 img.src = '';

2 给元素条件class

iframe.classList.add('print-iframe')

3 设置style

iframe.style.width='100%'

4 设置标签属性


const linkTag = document.createElement('link')
// link标签引入css文件的地址
linkTag.href = '/static/css/schedule_task.css'
// 设置link标签属性
linkTag.setAttribute('rel', 'stylesheet')

//设置style
iframe.setAttribute("style","position:absolute;width:0;height:0;left:0;top:0;");

5 添加元素

document.body.appendChild(iframe)

6 移除元素

const iframeEL=document.querySelector('.print-iframe');
        if(iframeEL){
          iframeEL.remove();
        }

示例:

for (const image of images) {
  const newImage = document.createElement('img');
  newImage.setAttribute('src', `images/${image}`);
  newImage.setAttribute('alt', alts[image]);
  thumbBar.appendChild(newImage);
  newImage.addEventListener('click', e => {
    displayedImage.src = e.target.src;
    displayedImage.alt = e.target.alt;
  });
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值