jq输出文本_jQuery设置与获取HTML、文本与值实例

本文通过示例介绍了如何使用jQuery获取和设置HTML元素的HTML内容、文本内容以及value值,包括文本框的焦点和失去焦点时的行为,以及单选框、多选框和下拉框的选择状态操作。
摘要由CSDN通过智能技术生成

//

$(function(){

//获取

元素的HTML代码

$("input:eq(0)").click(function(){

alert(  $("p").html() );

});

//获取

元素的文本

$("input:eq(1)").click(function(){

alert(  $("p").text() );

});

//设置

元素的HTML代码

$("input:eq(2)").click(function(){

$("p").html("你最喜欢的水果是?");

});

//设置

元素的文本

$("input:eq(3)").click(function(){

$("p").text("你最喜欢的水果是?");

});

//设置

元素的文本

$("input:eq(4)").click(function(){

$("p").text("你最喜欢的水果是?");

});

//获取按钮的value值

$("input:eq(5)").click(function(){

alert( $(this).val() );

});

//设置按钮的value值

$("input:eq(6)").click(function(){

$(this).val("我被点击了!");

});

});

//]]>

www.jbxue.com

//

$(function(){

$("#address").focus(function(){         // 地址框获得鼠标焦点

var txt_value =  $(this).val();   // 得到当前文本框的值

if(txt_value=="请输入邮箱地址"){

$(this).val("");              // 如果符合条件,则清空文本框内容

}

});

$("#address").blur(function(){          // 地址框失去鼠标焦点

var txt_value =  $(this).val();   // 得到当前文本框的值

if(txt_value==""){

$(this).val("请输入邮箱地址");// 如果符合条件,则设置内容

}

})

$("#password").focus(function(){

var txt_value =  $(this).val();

if(txt_value=="请输入邮箱密码"){

$(this).val("");

}

});

$("#password").blur(function(){

var txt_value =  $(this).val();

if(txt_value==""){

$(this).val("请输入邮箱密码");

}

})

});

//]]>

//

$(function(){

$("#address").focus(function(){         // 地址框获得鼠标焦点

var txt_value =  $(this).val();   // 得到当前文本框的值

if(txt_value==this.defaultValue){

$(this).val("");              // 如果符合条件,则清空文本框内容

}

});

$("#address").blur(function(){          // 地址框失去鼠标焦点

var txt_value =  $(this).val();   // 得到当前文本框的值

if(txt_value==""){

$(this).val(this.defaultValue);// 如果符合条件,则设置内容

}

})

$("#password").focus(function(){

var txt_value =  $(this).val();

if(txt_value==this.defaultValue){

$(this).val("");

}

});

$("#password").blur(function(){

var txt_value =  $(this).val();

if(txt_value==""){

$(this).val(this.defaultValue);

}

})

});

//]]>

//

$(function(){

//设置单选下拉框选中

$("input:eq(0)").click(function(){

$("#single").val("");

});

//设置多选下拉框选中

$("input:eq(1)").click(function(){

$("#multiple").val(["选择2号", "选择3号"]);

});

//设置单选框和多选框选中

$("input:eq(2)").click(function(){

$(":checkbox").val(["check2","check3"]);

$(":radio").val(["radio2"]);

});

});

//]]>

//

$(function(){

//设置单选下拉框选中

$("input:eq(0)").click(function(){

$("#single option").removeAttr("selected");  //移除属性selected

$("#single option:eq(1)").attr("selected",true); //设置属性selected

});

//设置多选下拉框选中

$("input:eq(1)").click(function(){

$("#multiple option").removeAttr("selected");  //移除属性selected

$("#multiple option:eq(2)").attr("selected",true);//设置属性selected

$("#multiple option:eq(3)").attr("selected",true);//设置属性selected

});

//设置单选框和多选框选中

$("input:eq(2)").click(function(){

$(":checkbox").removeAttr("checked"); //移除属性checked

$(":radio").removeAttr("checked"); //移除属性checked

$(":checkbox[value=check2]").attr("checked",true);//设置属性checked

$("[value=check3]:checkbox").attr("checked",true);//设置属性checked

$("[value=radio2]:radio").attr("checked",true);//设置属性checked

});

});

//]]>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值