用javascript添加控件自定义属性

1.为HTML元素添加一自定义的属性非常方便,只须将其加到尖括号中即可,与内置属性地位相等。

如我们要为TextBox元素添加属性idvalue

< input  type ="text"  id ="txtInput"  name ="txtInput"  value ="自定义文本" >
只须在原来的控件后面加上: idvalue=”…”, 成为 :
< input  type ="text"  id ="txtInput"  name ="txtInput"  value ="自定义文本"  idvalue ="自定义值" >

idvalue即可正式成为txtInput的属性,地位与其他属性相等。

如以下例子,在IE6中调试通过:

<html>
<head>
    
<title>自定义属性</title>
    
<script language="javascript">
            
function showText()
            
{
                alert(document.getElementById(
"txtInput").value);
            }

            
            
function showValue()
            
{
                alert(document.getElementById(
"txtInput").idvalue);
            }

 
    
</script>
</head>
<body>
    
<input type="text" id="txtInput" name="txtInput" value="自定义文本" idvalue="自定义值">
    
<input type="button" id="btnShowText" name="btnShowText" value="显示文本内容" onclick="showText();">
    
<input type="button" id="btnShowValue" name="btnShowValue" value="显示文本值" onclick="showValue();">
</body>
</html>

idvalueFirefox中却不能通过,主要是因为Firefox控制严格,所以这些自定义属性不能认识。经过调试,只能用document.getElementById("txtInput").attributes["idvalue"].nodeValue取得,该方法在IE中也可使用。所以,同时适用IEFirefox的代码为:

<html>
<head>
    
<title>自定义属性</title>
    
<script language="javascript">
            
function showText()
            
{
                alert(document.getElementById(
"txtInput").value);
            }

            
            
function showValue()
            
{
                alert(document.getElementById(
"txtInput").attributes["idvalue"].nodeValue);
            }

 
    
</script>
</head>
<body>
    
<input type="text" id="txtInput" name="txtInput" value="自定义文本" idvalue="自定义值">
    
<input type="button" id="btnShowText" name="btnShowText" value="显示文本内容" onclick="showText();">
    
<input type="button" id="btnShowValue" name="btnShowValue" value="显示文本值" onclick="showValue();">
</body>
</html>

2.js赋值

前面说过为HTML元素添加自定义的属性,是通过手动在HTML控件中加上,其实可以在javascript中动态添加:如有一文本框:

< input  type ="text"  id ="txtInput"  name ="txtInput"  value ="自定义文本" >

如想增加idvalue属性(值为自定义值”),可以在javascript中这样写:

var  txt  =  document.getElementById( " txtInput " );
txt.setAttribute(
" idvalue " , " 自定义值 " );

setAttribute中第一个参数是指明自定义属性的名称,第二个参数是初始值

代码如下:

<html>
<head>
    
<title>用javascript添加控件自定义属性</title>
    
<script language="javascript">
        
function addCustomAttribute()
        
{
            
var txt = document.getElementById("txtInput");
            txt.setAttribute(
"idvalue","自定义值");
        }

        
        
function showIdValue()
        
{
                
var txt = document.getElementById("txtInput");
                alert(txt.attributes[
"idvalue"].nodeValue);
        }

    
</script>
</head>
<body onload="addCustomAttribute();">
    
<input type="text" id="txtInput" name="txtInput" value="自定义文本">
    
<input type="button" value="显示idValue" onclick="showIdValue();">
</body>
</html>

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用JavaScript代码来创建自定义的日期选择器控件的一个示例: ```html <input type="text" id="datePicker" placeholder="选择日期"> <script> // 获取input元素和当前日期 var datePicker = document.getElementById('datePicker'); var today = new Date(); // 给input元素添加click事件,当用户点击时弹出日期选择器 datePicker.addEventListener('click', function() { // 创建日期选择器的容器元素 var container = document.createElement('div'); container.className = 'date-picker-container'; // 创建年份选择器 var yearSelector = document.createElement('select'); for (var i = today.getFullYear() - 100; i <= today.getFullYear() + 100; i++) { var option = document.createElement('option'); option.value = i; option.text = i; yearSelector.appendChild(option); } yearSelector.value = today.getFullYear(); // 创建月份选择器 var monthSelector = document.createElement('select'); for (var i = 1; i <= 12; i++) { var option = document.createElement('option'); option.value = i; option.text = i < 10 ? '0' + i : i; monthSelector.appendChild(option); } monthSelector.value = today.getMonth() + 1; // 创建日期选择器 var daySelector = document.createElement('select'); var year = yearSelector.value; var month = monthSelector.value; var daysInMonth = new Date(year, month, 0).getDate(); for (var i = 1; i <= daysInMonth; i++) { var option = document.createElement('option'); option.value = i; option.text = i < 10 ? '0' + i : i; daySelector.appendChild(option); } daySelector.value = today.getDate(); // 将选择器添加到容器元素中 container.appendChild(yearSelector); container.appendChild(monthSelector); container.appendChild(daySelector); // 给选择器添加change事件,当用户选择日期时更新input元素的value属性 yearSelector.addEventListener('change', updateValue); monthSelector.addEventListener('change', updateValue); daySelector.addEventListener('change', updateValue); // 将容器元素添加到页面中 document.body.appendChild(container); // 更新input元素的value属性 function updateValue() { var year = yearSelector.value; var month = monthSelector.value; var day = daySelector.value; datePicker.value = year + '-' + month + '-' + day; } // 点击任意位置关闭日期选择器 document.addEventListener('click', function(e) { if (!container.contains(e.target) && e.target != datePicker) { document.body.removeChild(container); } }); }); </script> <style> .date-picker-container { position: absolute; top: 40px; left: 0; right: 0; margin: 0 auto; z-index: 9999; text-align: center; background-color: #fff; border: 1px solid #ccc; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); padding: 10px; } select { font-size: 16px; padding: 5px; margin: 0 10px; } </style> ``` 在上面的示例中,我们使用JavaScript代码创建了一个日期选择器控件,并且在用户选择日期时自动更新了input元素的value属性。我们通过创建三个选择器元素来让用户选择年、月、日,然后在这些选择器元素的change事件中更新input元素的value属性。同时,我们使用CSS样式对日期选择器控件进行了一些简单的美化,使其更加易于使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值