document.createElement創建控件

本文介绍了如何使用JavaScript动态地创建和插入HTML元素到页面中,包括链接、图片、单选按钮等,并展示了如何通过脚本加载外部JavaScript文件实现定时刷新功能。

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

<html>
<BODY> 
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var A = document.createElement("A"); 
A.setAttribute("href","#"); 
A.appendChild(document.createTextNode("link")); 
document.getElementsByTagName("BODY")[0].appendChild(A); 
//--> 
</SCRIPT> 
</BODY>
</html>

===========================================================

<html>
<meta http-equiv="Content-Language" c>
<head>
<title></title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript"> 
oImage=document.createElement("IMG")
   oImage.src="e:/web/net/images/toqq.gif"
   document.body.appendChild(oImage)</SCRIPT> 
<form id="Form1" runat="server" >
</form>
</body>
</html>

=============================

 

<HTML>
<HEAD>
<SCRIPT>
function createRadioButton(){
    // Create radio button object with value="First Choice" and then insert
    // this element into the document hierarchy.
    var newRadioButton = document.createElement("<INPUT TYPE='RADIO' NAME='RADIOTEST' VALUE='First Choice'>")
    document.body.insertBefore(newRadioButton);
    // Create radio button object with value="Second Choice" and then insert
    // this element into the document hierarchy.
    newRadioButton = document.createElement("<INPUT TYPE='RADIO' NAME='RADIOTEST' VALUE='Second Choice'>")
    document.body.insertBefore(newRadioButton);
}
</SCRIPT>
</HEAD>

<BODY>
<INPUT TYPE="BUTTON" ONCLICK="createRadioButton()" VALUE="Create two Radio Buttons"><BR>

<INPUT TYPE="BUTTON" ONCLICK="alert ( document.body.outerHTML )" VALUE="Click here to see HTML">

<BODY>
</HTML>
====================================================

动态加载js文件 document.createElement

Posted on 2007-04-15 20:38 海浪空间 阅读(333) 评论(0)   编辑   收藏 所属分类: 转载

var Rash=true;
var msg="";
function norash()
{
if (confirm("确定要取消吗"))
Rash=false;
}
 function rashit()
{
setInterval('getrss()',Inttime);
}
function getrss()
{
        if (Rash==true)
        {
        head=document.getElementsByTagName('head').item(0);
        script=document.createElement('script');
        script.src='INCLUDE/AutoUpdate.asp';
        script.type='text/javascript';
        script.defer=true;
        void(head.appendChild(script));
        window.status=msg;
        }
}
rashit();

========================================

var mydiv=document.createElement("div");//创建一个元素.
mydiv.setAttribute("id","macnie");//赋值给这个元素一个ID。
a1.removeNode(true)  //删除这个元素.

``` // 更新分页控件 function updatePagination(totalPages, currentPage) { const paginationContainer = document.getElementById('pagination'); paginationContainer.innerHTML = ''; if (currentPage > 1) { const prevBtn = document.createElement('button'); prevBtn.textContent = '上一页'; prevBtn.onclick = () => { currentPage--; loadData(); }; paginationContainer.appendChild(prevBtn); } let startPage, endPage; if (totalPages <= 5) { startPage = 1; endPage = totalPages; } else { startPage = Math.max(1, currentPage - 2) endPage = Math.min(totalPages, currentPage + 2); } if (startPage > 1) { const firstPageBtn = document.createElement('button'); firstPageBtn.textContent = '1'; firstPageBtn.onclick = () => { currentPage = 1; loadData(); }; paginationContainer.appendChild(firstPageBtn); if (startPage > 2) { const ellipsisStart = document.createElement('span'); ellipsisStart.textContent = '...'; paginationContainer.appendChild(ellipsisStart); } } for (let i = startPage; i <= endPage; i++) { const pageBtn = document.createElement('button'); pageBtn.textContent = i; if (i === currentPage) { pageBtn.style.backgroundColor = '#ddd'; } pageBtn.onclick = () => { currentPage = i; loadData(); }; paginationContainer.appendChild(pageBtn); } if (endPage < totalPages) { if (endPage < totalPages - 1) { const ellipsisEnd = document.createElement('span'); ellipsisEnd.textContent = '...'; paginationContainer.appendChild(ellipsisEnd); } const lastPageBtn = document.createElement('button'); lastPageBtn.textContent = totalPages; lastPageBtn.onclick = () => { currentPage = totalPages; loadData(); }; paginationContainer.appendChild(lastPageBtn); } if (currentPage < totalPages) { const nextBtn = document.createElement('button'); nextBtn.textContent = '下一页'; nextBtn.onclick = () => { currentPage++; loadData(); }; paginationContainer.appendChild(nextBtn); } // 添加跳转输入框 const gotoInput = document.createElement('input'); gotoInput.type = 'number'; gotoInput.min = '1'; gotoInput.max = totalPages; gotoInput.placeholder = '跳转到...'; gotoInput.style.marginLeft = '10px'; gotoInput.style.width = '50px'; paginationContainer.appendChild(gotoInput); const gotoBtn = document.createElement('button'); gotoBtn.textContent = '跳转'; gotoBtn.onclick = () => { const page = parseInt(gotoInput.value, 10); if (page >= 1 && page <= totalPages) { currentPage = page; loadData(); } }; paginationContainer.appendChild(gotoBtn); }```A form field element should have an id or name attribute
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值