1.确认用户的选择:
比如在用户删除之前弹出窗体再次确认是否删除 如果是进入另一个jsp界面 如果不是则不动
js代码:
<script type="text/javascript">
function confirmAct() {
if(confirm("确认删除文章?")){
return true;
}
else{
return false;
}
}
</script>
html代码:
<a href="test.jsp" οnclick="return confirmAct();">执行操作</a>
2.使用多级条件
按每个按钮弹出相应的值
当加载页面时调用init()函数
js代码:
<script type="text/javascript">
window.οnlοad=init;
function init() {
document.getElementById("lincoln").οnclick=saysome;
document.getElementById("kenndey").οnclick=saysome;
document.getElementById("nixon").οnclick=saysome;
}
function saysome() {
switch (this.id){
case "lincoln":
alert("lincoln");
break;
case "kenndey":
alert("kenndey");
break;
case "nixon":
alert("nixon");
break;
}
}
</script>
html代码:
<form action="#">
<input type="button" id="lincoln" value="lincoln"/>
<input type="button" id="kenndey" value="kenndey"/>
<input type="button" id="nixon" value="nixon"/>
</form>
3.选择并转移导航菜单
通过菜单进入不同大的目的地,不需要Go按钮
js代码
window.onload = initAll;
function initAll(){
//属性值设为0,显示select a topic
document.getElementById("newLoc").selectedIndex = 0;
//脚本在菜单选择发生改变时,调用change函数
document.getElementById("newLoc").onchange = change;
}
function change(){
//newLoc变量查找访问者在菜单中选择的值
var newLoc = document.getElementById("newLoc");
//得到值之后获得对应菜单项的值,即希望跳转网页的名称,赋值给newPage
var newPage = newLoc.options[newLoc.selectedIndex].value;
if(newPage != ""){
//如果值不为空跳转到指定的url
window.location = newPage;
}
}
html代码:
nosript不支持javascript浏览器会出现按钮
<form action="#">
<select id="newLoc">
<option selected>Select a topic</option>
<option value="s1.html">Cross-checking fields</option>
<option value="test.jsp">Working with radio buttons</option>
</select>
<noscript>
<input type="submit" value="Go There!">
</noscript>
</form>