日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Jquery和javascript常用技巧

發(fā)布時(shí)間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jquery和javascript常用技巧 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??????? var objSel = document.getElementById("selOp");?
?????????
??????? //這是獲取值?
??????? alert("當(dāng)前值: " + objSel.value);?
?????????
??????? //這是獲取文本?
??????? alert("當(dāng)前文本: " + objSel.options(objSel.selectedIndex).text);?

?

?

增加復(fù)選框套路
var e = document.createElement("input");
??e.type = "checkbox";
??e.value = result.content[i].id;
??fbox.appendChild(e);
??fbox.appendChild(document.createTextNode(result.content[i].name+"??? "));


刪除子節(jié)點(diǎn)
var fbox = document.getElementById("chbox");
fbox.innerHTML ='';??

<div id="chbox"></div>

?

動(dòng)態(tài)添加下拉框節(jié)點(diǎn)

var eles = document.forms['theForm'].elements;
?? eles['goodsList'].length = 1;
?? for (i = 0; i < result.content.length; i++)
?? {
???? var opt = document.createElement('OPTION');
???? opt.value = result.content[i].id;
???? opt.text? = result.content[i].name;
???? eles['goodsList'].options.add(opt);
?? }

?

?

中文檢測(cè)的正則

function?? checkStr(str)?? //中文值檢測(cè)
{?
var reg =/^[\u4e00-\u9fff]*$/;
????? if(!reg.test(str)){
?????? return true;
????? }
????? return false;
}


注意網(wǎng)上的 [\u4e00-\u9fa5]區(qū)間不對(duì)

?

?var reg= /^[1][3458]\d{9}$/; //驗(yàn)證手機(jī)號(hào)碼?
????? if(!reg.test(str)){
?????? return false;
????? }
????? return true;

?

//jquery bug IE7,8不能取到單選按鈕的選中狀態(tài),IE9 可以
//if($("#rd_getsupplierinfo:checked").get(0).checked == true)

這個(gè)辦法可以
if($("#rd_getsupplierinfo").attr("checked")=="checked")

?

//jquery bug IE7,8不支持

$("span[class*='allprice']").each(function() {

只能用

?$("span").each(function(){?? if($(this).attr("class")=="allprice"){

遍歷

$("button").click(function(){? $("li").each(function(){??? alert($(this).text())? });});

?

文本框立輸入值立即觸發(fā)事件

<input type="number" min="0" step="1" name="goods_num" id="goods_number" size="10" value=""? οninput="alert('7')" onpropertychange="alert(1)" required="required"/>

?

動(dòng)態(tài)id
var id = 'one';
var el = $('#' + id);

?

遍歷

$("span[class*='price']").each(function()

?{??

?$('#count').text(parseInt($('#count').text())+parseInt($(this).text()));????????????

?});

?

$("select").each(
? function()
? {
??? if($(this).attr('class')=="brand")
????????? {
???????????? if($(this).val()=="0"||$(this).val()==null)
???????????????? {
???????????????????? validator.addErrorMsg("不能為空");
???????????????? }
??????????????
????????? }
?
? });

遍歷下拉框

?

?

jquery驗(yàn)證

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

?

下面分類別的簡單介紹一下jQuery選擇器
1、基本選擇器:通過標(biāo)簽元素id、class、標(biāo)簽名等來查找DOM元素
$("#id名"),如$("#test")是選取了id為test的標(biāo)簽節(jié)點(diǎn)
$("標(biāo)簽名"),如$("p")是選取了所有的P標(biāo)簽節(jié)點(diǎn)
$(".class名"),如$(".test")是選取了所有class為test的標(biāo)簽節(jié)點(diǎn)
$("*"),如$("*")是選取所有的標(biāo)簽元素
$("標(biāo)簽名1,標(biāo)簽名2,..."),如$("div,span,p.myClass")是選取所有<div>,<span>和擁有class為myClass的<p>的一線標(biāo)簽元素。
2、層次選擇器:通過DOM元素的層次關(guān)系來獲取特定元素,包括后代元素、子元素、相依元素、兄弟元素等。
$("標(biāo)簽名 標(biāo)簽名"),如$("div span")選取<div>里所有的<span>元素
$("parent child"),如$("div>span")選取<div>元素下元素名是<span>的子元素
$('prev+next')等價(jià)$('prev').next('next'),如$('.one+div')等價(jià)$('.one').next('div')是選取class為one的下一個(gè)<div>標(biāo)簽元素
$('prev~siblings')等價(jià)$('prev').nextAll('div'),如('#two~div')等價(jià)$('#two').nextAll('div')是選取id為two的元素后面的所有<div>兄弟元素
3、過濾選擇器:主要是通過一些過濾規(guī)則來篩選DOM元素,過濾規(guī)則與CSS中的偽類選擇器語法相同,即選擇器都以一個(gè)冒號(hào)(:)開頭
$("標(biāo)簽元素:first"),如$("div:first")是選取所有<div>元素中第一個(gè)<div>元素

?

js父節(jié)點(diǎn)

this.parentNode.id
this.parentNode.parentNode.id

?

增加節(jié)點(diǎn)

$("p").before( $("b");在每個(gè)匹配的元素之前插入內(nèi)容。
表示p的前面是b,也就是b要插到p的前面。

$("p").insertBefore("b");表示將p插入到b的前面

$("p").insertAfter("#foo");把所有匹配的元素插入到另一個(gè)、指定的元素元素集合的后面。

$("p").append("<b>Hello</b>");向每個(gè)匹配的元素內(nèi)部追加內(nèi)容,這個(gè)操作與對(duì)指定的元素執(zhí)行

appendChild方法,將它們添加到文檔中的情況類似。

$("p").remove();
從DOM中刪除所有匹配的元素。

?

?

span

$("#fok").text("aaaaaaaa");

$("#fok").html("aaaaaaaa");

$(document).ready(function(){????
??? $("#resetbtn").click(function(){?
??????? $("#user_id").val("");//清空?
??????? $("#realname").val("www");//賦值?
?? });?
});

?

設(shè)置select的value值為4的項(xiàng)選中: $("#slc1 ").val(4);?

?

頁面有多個(gè)下拉框,需要遍歷判斷

非主動(dòng)觸發(fā),例如統(tǒng)一驗(yàn)證

$("select").each(
? function()
? {alert($(this).attr('name'));
?? alert($(this).val())
alert($(this).text());當(dāng)前所有text
alert($(this).find("option:selected").text());當(dāng)前選中text
? });

?

?

主動(dòng)觸發(fā)

<select name="s_id[{$goods.goods_id}]" οnchange="changBrandEach(this)">
function changBrandEach(sld)
??????????????? {
??????????????????? alert($(sld).val());

js獲取id和name
sld.id?? sld.name

單個(gè)
var s_id = $("#s_id ").val();

說明如果是動(dòng)態(tài)的就去掉#和""

?

jquery獲得下拉框的值
獲取Select :
?獲取select 選中的 text :
?? $("#ddlRegType").find("option:selected").text();
?
?獲取select選中的 value:
?? $("#ddlRegType ").val();
?
?獲取select選中的索引:
???? $("#ddlRegType ").get(0).selectedIndex;
?
設(shè)置select:
?設(shè)置select 選中的索引:
???? $("#ddlRegType ").get(0).selectedIndex=index;//index為索引值
?
?設(shè)置select 選中的value:
??? $("#ddlRegType ").attr("value","Normal“);
??? $("#ddlRegType ").val("Normal");
??? $("#ddlRegType ").get(0).value = value;
?
?設(shè)置select 選中的text:
var count=$("#ddlRegType option").length;
? for(var i=0;i<count;i++)?
???? {?????????? if($("#ddlRegType ").get(0).options[i].text == text)?
??????? {?
??????????? $("#ddlRegType ").get(0).options[i].selected = true;?
?????????
??????????? break;?
??????? }?
??? }
?
$("#select_id option[text='jQuery']").attr("selected", true);
?
設(shè)置select option項(xiàng):
?
?$("#select_id").append("<option value='Value'>Text</option>");? //添加一項(xiàng)option
?$("#select_id").prepend("<option value='0'>請(qǐng)選擇</option>"); //在前面插入一項(xiàng)option
?$("#select_id option:last").remove(); //刪除索引值最大的Option
?$("#select_id option[index='0']").remove();//刪除索引值為0的Option
?$("#select_id option[value='3']").remove(); //刪除值為3的Option
?$("#select_id option[text='4']").remove(); //刪除TEXT值為4的Option

?

?

動(dòng)態(tài)增加下拉選項(xiàng)


var eles = document.forms['theForm'].elements;
??????????????????? eles[brand_id].length = 0;//這句很重要,否則會(huì)以疊加的形式出現(xiàn)
??????????????????? for (i = 0; i < result.content.length; i++)
??????????????????? {
??????????????????? var opt = document.createElement('OPTION');
??????????????????? opt.value = result.content[i].brand_id;
??????????????????? opt.text? = result.content[i].brand_name;
??????????????????? eles[brand_id].options.add(opt);
??????????????????? }?
清空 Select:
$("#ddlRegType ").empty();

?

1,下拉框:
var cc1? = $(".formc select[@name='country']? option[@selected]").text(); //得到下拉菜單的選中項(xiàng)的文本(注意中間有空格)
var cc2 = $('.formc? select[@name="country"]').val();? //得到下拉菜單的選中項(xiàng)的值
var cc3 = $('.formc? select[@name="country"]').attr("id");? //得到下拉菜單的選中項(xiàng)的ID屬性值
$("#select").empty();//清空下拉框//$("#select").html('');
$("<option? value='1'>1111</option>").appendTo("#select")//添加下拉框的option
稍微解釋一下:
1.select[@name='country']? option[@selected] 表示具有name 屬性,
并且該屬性值為'country' 的select元素 里面的具有selected? 屬性的option? 元素;
可以看出有@開頭的就表示后面跟的是屬性。

2,單選框:
$("input[@type=radio][@checked]").val();? //得到單選框的選中項(xiàng)的值(注意中間沒有空格)
$("input[@type=radio][@value=2]").attr("checked",'checked');? //設(shè)置單選框value=2的為選中狀態(tài).(注意中間沒有空格)

3,復(fù)選框:
$("input[@type=checkbox][@checked]").val();? //得到復(fù)選框的選中的第一項(xiàng)的值
$("input[@type=checkbox][@checked]").each(function(){? //由于復(fù)選框一般選中的是多個(gè),所以可以循環(huán)輸出
? alert($(this).val());
? });

$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined){}? //判斷是否已經(jīng)打勾

javascript 獲得下拉框選中值
var index = document.getElementById("DropDownList1").selectedIndex;
var v=document.getElementById("DropDownList1").options[index].value;

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

下拉框值選中

/獲取第一個(gè)option的值?
$('#test option:first').val();?
//最后一個(gè)option的值?
$('#test option:last').val();?
//獲取第二個(gè)option的值?
$('#test option:eq(1)').val();?
//獲取選中的值?
$('#test').val();?
$('#test option:selected').val();?
//設(shè)置值為2的option為選中狀態(tài)?
$('#test').attr('value','2');?
//設(shè)置第一個(gè)option為選中?
$('#test option:last').attr('selected','selected');?
$("#test").attr('value' , $('#test option:last').val());?
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val());?
//獲取select的長度?
$('#test option').length;?
//添加一個(gè)option?
$("#test").append("<option value='9'>ff</option>");?
$("<option value='9'>ff</option>").appendTo("#test");?
//添除選中項(xiàng)?
$('#test option:selected').remove();?
//指定項(xiàng)選中?
$('#test option:first').remove();?
//指定值被刪除?
$('#test option').each(function(){?
if( $(this).val() == '5'){?
$(this).remove();?
}?
});?
$('#test option[value=5]').remove();?

//獲取第一個(gè)Group的標(biāo)簽?
$('#test optgroup:eq(0)').attr('label');?
//獲取第二group下面第一個(gè)option的值?
$('#test optgroup:eq(1) :option:eq(0)').val();?

獲取select中選擇的text與value相關(guān)的值?

獲取select選擇的Text : var checkText=$("#slc1").find("option:selected").text();?
獲取select選擇的value:var checkValue=$("#slc1").val();?
獲取select選擇的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex;?
獲取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index");?

設(shè)置select選擇的Text和Value?

設(shè)置select索引值為1的項(xiàng)選中:$("#slc1 ").get(0).selectedIndex=1;?
設(shè)置select的value值為4的項(xiàng)選中: $("#slc1 ").val(4);?
設(shè)置select的Text值為JQuery的選中:?
$("#slc1 option[text='jQuery']").attr("selected", true);?
PS:特別要注意一下第三項(xiàng)的使用哦??纯碕Query的選擇器功能是如此地強(qiáng)大呀!?

添加刪除option項(xiàng)?

為select追加一個(gè)Option(下拉項(xiàng))?
$("#slc2").append("<option value='"+i+"'>"+i+"</option>");?
為select插入一個(gè)option(第一個(gè)位置)?
$("#slc2").prepend("<option value='0'>請(qǐng)選擇</option>");?
PS: prepend 這是向所有匹配元素內(nèi)部的開始處插入內(nèi)容的最佳方式。?
刪除select中索引值最大option(最后一個(gè))?
$("#slc2 option:last").remove();?
刪除select中索引值為0的option(第一個(gè))?
$("#slc2 option[index='0']").remove();?
刪除select中value='3'的option?
$("#slc2 option[value='3']").remove();?
刪除select中text='4'的option?
$("#slc2 option[text='3']").remove();

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

驗(yàn)證

validator = new Validator("theForm");???

validator.required("goods_number","商品數(shù)量不能為空");???

validator.isNumber("goods_number","商品數(shù)量必須為數(shù)字");???

if($("#rd_tuan_price").attr("checked")==undefined)??? {???????

validator.required("tuan_price_my","自定義團(tuán)購價(jià)格不能為空");???????

validator.isNumber("tuan_price_my","自定義團(tuán)購價(jià)格必須為數(shù)字");??? }???????

?if(validator.passed()==false)???

?{???????????

?return false;???

?}

總結(jié)

以上是生活随笔為你收集整理的Jquery和javascript常用技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。