第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關(guān)于js對象的問題

關(guān)于js對象的問題

猛跑小豬 2019-03-13 14:15:22
function judge(id,pwd){    if(id.value==null)    {        document.getElementById("tip").innerHTML="請輸入賬號";    }    else    {        document.getElementById("tip").innerHTML="請輸入密碼";    }}以上是函數(shù)部分。Q1:<form action="" method="post" name="register">    <input type="text" id="id" placeholder="請輸入賬號" class="form-control" style="margin-bottom: 5%;">     <input type="password" id="pwd" placeholder="請輸入密碼" class="form-control" style="margin-top: 5%; margin-bottom: 3%;">    <span class="tip" id="tip"></span>    <input type="submit" value="登錄" class="button" onsubmit="judge(document.getElementById('id'),document.getElementById('pwd'))"></form>為什么不能實現(xiàn)元素處出現(xiàn)"請輸入賬號"的效果?Q2:<form action="" method="post" name="register">    <input type="text" id="id" placeholder="請輸入賬號" class="form-control" style="margin-bottom: 5%;">     <input type="password" id="pwd" placeholder="請輸入密碼" class="form-control" style="margin-top: 5%; margin-bottom: 3%;">    <span class="tip" id="tip"></span>        <!--以下為修改代碼-->    <button class="button" onclick="judge(document.getElementById('id'),document.getElementById('pwd'))">登錄</button></form>這樣能實現(xiàn)效果,但是頁面會重新加載,效果一閃而過。為什么?如何調(diào)整?Q3:<form action="" method="post" name="register">    <input type="text" id="id" placeholder="請輸入賬號" class="form-control" style="margin-bottom: 5%;">     <input type="password" id="pwd" placeholder="請輸入密碼" class="form-control" style="margin-top: 5%; margin-bottom: 3%;">    <span class="tip" id="tip"></span>        <!--以下為修改代碼-->    <button class="button" onclick="judge(id,pwd)">登錄</button></form>為什么效果同2?id名也能引用一個對象?Q4:<form action="" method="post" name="register">        <!--將id改為name-->    <input type="text" name="id" placeholder="請輸入賬號" class="form-control" style="margin-bottom: 5%;">     <input type="password" name="pwd" placeholder="請輸入密碼" class="form-control" style="margin-top: 5%; margin-bottom: 3%;">    <span class="tip" id="tip"></span>    <button class="button" onclick="judge(id,pwd)">登錄</button></form>為什么效果同上?難道name也能引用一個對象?
查看完整描述

4 回答

?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗 獲得超4個贊

很好奇,你的judge函數(shù)里根本就沒有 pwd,為何參數(shù)要傳他。
產(chǎn)生問題的原因和 name id 沒有太大的關(guān)系。

首先,這些東西都寫在了form表單里面,這里action填寫的是指的是提交到當(dāng)前頁。

Q1 登錄按鈕式一個 submit ,點擊登錄按鈕的時候,執(zhí)行的是表單的 submit 事件,也就是直接提交表單數(shù)據(jù)了,不會執(zhí)行 onclick事件,頁面刷新。

Q2 點擊button的時候,執(zhí)行了 onclick事件,同時也執(zhí)行了 表單的 submit事件,前者改變效果,后者提交自然刷新頁面,也就是出現(xiàn)一閃而過的樣子。

Q3 id 直接引用,有時候可以,不常用,或者不輕易用,而且瀏覽器不知道有沒有兼容性,我測試的時可以用。對于這些怪異的現(xiàn)象,不用太理會,按照標(biāo)準(zhǔn)來。

Q4 name 一般式留給提交表單之后的后臺程序引用的。

其次,盡量不要把 js 代碼 寫在 html 里面,分離結(jié)構(gòu) 樣式 行為。這種html里面的 onclick 寫法盡量少用。


查看完整回答
反對 回復(fù) 2019-04-13
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗 獲得超2個贊

"placeholder"屬性是h5中新定義的 你要考慮到你所用瀏覽器的兼容性

我在chrome中測試的效果如下:

https://img1.sycdn.imooc.com//5cb18c8b00013bfd04170167.jpg

查看完整回答
反對 回復(fù) 2019-04-13
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗 獲得超8個贊

id.value不對


var val = document.getElementById('id').getAttribute('value');

if( val==''){

    document.getElementById("tip").innerHTML="請輸入賬號";

}

else{

    document.getElementById("tip").innerHTML="請輸入密碼";

}

Q2:在form之前加個iframe,將form的action在何處打開指向這個iframe,就可以解決表單提交后刷新頁面的情況。


<iframe name="frame" style="display:none;"></iframe> 

然后還要在form中添加一個target屬性,屬性值設(shè)置為iframe中的name屬性值即可。


<form target="frame"  xxx=""  xxx="">

    ... 

</form>


查看完整回答
反對 回復(fù) 2019-04-13
  • 4 回答
  • 0 關(guān)注
  • 551 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號