1.动态添加元素
JS方法:
<p id="demo">This is a paragraph.</p> <button type="button" onclick="displayDate()">Display Date</button> <script> function displayDate(){ document.getElementById("demo").innerHTML=Date(); } </script>
2.按钮失效
JS方法:设置disabled属性为true即为不可用状态。
页面:<input type="button" value="提交" id="btn">
document.getElementByIdx("btn").disabled=true;
Jquery方法:
$("#btn").attr("disabled", true);
3.禁止alert弹窗
JS方法:window.alert = function(){ return false; }
4.javascript中的innerHTML
innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容;
如:<div id="aa">这是内容</div> ,我们可以通过 document.getElementById('aa').innerHTML 来获取id为aa的对象的内嵌内容;
也可以对某对象插入内容,如 document.getElementById('abc').innerHTML='这是被插入的内容'; 这样就能向id为abc的对象插入内容
。
拓展:
jquery禁用a标签,jquery禁用按钮click点击
jquery禁用a标签方法1:
$(document).ready(function () { $("a").each(function () { var textValue = $(this).html(); if (textValue == "XX概况" || textValue == "服务导航") { $(this).css("cursor", "default"); $(this).attr('href', '#'); //修改<a>的 href属性值为 # 这样状态栏不会显示链接地址 $(this).click(function (event) { event.preventDefault(); // 如果<a>定义了 target="_blank“ 需要这句来阻止打开新页面 }); } }); });
jquery禁用a标签方法2
$('a.tooltip').live('click', function(event) { alert("抱歉,已停用!"); event.preventDefault(); });
jquery禁用a标签方法3
$(function(){ $('.disableCss').removeAttr('href');//去掉a标签中的href属性 $('.disableCss').removeAttr('onclick');//去掉a标签中的onclick事件 });
jquery控制按钮的禁用与启用
$('#button').attr('disabled',"true");添加disabled属性 $('#button').removeAttr("disabled"); 移除disabled属性
.