优快云超级大笨狼的精简代码集

本文介绍了一种在网页上动态操作HTML元素的有效方法,包括如何用简洁的代码删除表格内容、增加新行、在DIV中添加或移除表格,以及对SELECT元素进行增删选项的快速操作。此外还列举了Document对象和DIV对象常用的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,动态删除Table 里面内容技巧,不需要写太多代码,一行: 
tb.removeNode(true) 

2,动态增加行,除了CreateElement方法,还可以这样比较短小: 


 

3,在DIV中动态增加Table 



 

4,在DIV中删除Table,简单只要Div.innerHTML=""就可以。 

以上是部分实用相对短小的代码,当然有其他各种办法实现,不过一般都比上面的长,比如组合使用DIV对象的 insertAdjacentHTML 方法等,在不同需要下使用不同方法,前提是研究好浏览器模型 的各种对象的方法属性。尤其是熟悉CSS+ HTML就会做的很酷。就JS语言本身来说要求不高。 

5,如果是IE浏览器,想创建什么就创建什么。
Div.insertAdjacentHTML("afterBegin",HTML); 

6,select 操作最简短版 
  •   
     
     


    最快速删除所有 option 的方法: 
    document.formName.selectName.options.length = 0; //就这么简单


    以下是以Document对象为例,相关方法有: 

    Method Description 
    attachEvent 
    createAttribute 
    createComment 
    createDocumentFragment 
    createElement 
    createEventObject 
    createStyleSheet 
    createTextNode 
    detachEvent 
    getElementById 
    getElementsByName 
    getElementsByTagName 
    mergeAttributes 
    recalc 
    write 
    writeln 
    以DIV对象为例相关方法有: 
    addBehavior 
    appendChild 
    applyElement 
    attachEvent 
    clearAttributes 
    cloneNode 
    contains 
    detachEvent 
    getAdjacentText 
    getAttribute 
    getAttributeNode 
    getElementsByTagName 
    hasChildNodes 
    insertAdjacentElement 
    insertAdjacentHTML 
    insertAdjacentText 
    insertBefore 
    mergeAttributes 
    normalize 
    removeAttribute 
    removeAttributeNode 
    removeBehavior 
    removeChild 
    removeExpression 
    removeNode 
    replaceAdjacentText 
    replaceChild 
    replaceNode 
    setActive 
    setAttribute 
    setAttributeNode 
    setExpression 
  • 评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值