亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

在 html 中添加屬性時出錯。我該如何修復它?

在 html 中添加屬性時出錯。我該如何修復它?

MM們 2022-09-16 21:04:24
我試圖使用Skype與HTML和爪哇腳本進行聊天。這里的問題是我無法將屬性href添加到我的id中。這里我的代碼:function el(elementId, username, action) {    document.getElementById(elementId).setAttribute("href", "skype:" + username + "?" + action);}function buildLinkRefs() {    var username = document.getElementById("username").value;    el("call-btn", username, "call");    el("add-to-contacts-btn", username, "add");    el("view-profile-btn", username, "userinfo");    el("voice-email-btn", username, "voicemail");    el("chat-btn", username, "chat");    el("sendfile-btn", username, "sendfile");}document.getElementById("username").addEventListener("change", function () {    buildLinkRefs();}, false);buildLinkRefs();            <input type="text" id="username" value="echo123"/><br>    <br>    <a id="call-btn">Call</a> <br>    <a id="add-to-contacts-btn">Add to contacts</a> <br>    <a id="view-profile-btn">View User Profile</a> <br>    <a id="voice-email-btn">Voice Email</a> <br>    <a id="chat-btn">Start Chat</a> <br>    <a id="sendfile-btn">Send File</a> <br>
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

您使用了錯誤的事件偵聽器。文本輸入應使用“鍵控”,而不是“更改”


document.getElementById('username').addEventListener('keyup', buildLinkRefs );

因此,使用您的代碼,它應該如下所示:


function el(elementId, username, action) {

    document.getElementById(elementId).setAttribute("href", "skype:" + username + "?" + action);

}


function buildLinkRefs() {

    var username = document.getElementById("username").value;


    el("call-btn", username, "call");

    el("add-to-contacts-btn", username, "add");

    el("view-profile-btn", username, "userinfo");

    el("voice-email-btn", username, "voicemail");

    el("chat-btn", username, "chat");

    el("sendfile-btn", username, "sendfile");

}


document.getElementById("username").addEventListener("keyup", buildLinkRefs );


buildLinkRefs();  

<input type="text" id="username" value="echo123"/><br>

    <br>

    <a id="call-btn">Call</a> <br>

    <a id="add-to-contacts-btn">Add to contacts</a> <br>

    <a id="view-profile-btn">View User Profile</a> <br>

    <a id="voice-email-btn">Voice Email</a> <br>

    <a id="chat-btn">Start Chat</a> <br>

    <a id="sendfile-btn">Send File</a> <br>


查看完整回答
反對 回復 2022-09-16
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

問題是,您的函數在加載整個頁面之前被觸發,因此它看不到該元素,因為該元素不可用 。因此,有很多方法可以解決它 。只需將代碼放在html元素之后或下方,如下所示:username


<html>

 <head>

    <title>the title</title>

 </head>

     <body>

       <input type="text" id="username" value="echo123"/><br>

   <br>

    <a id="call-btn">Call</a> <br>

    <a id="add-to-contacts-btn">Add to contacts</a> <br>

    <a id="view-profile-btn">View User Profile</a> <br>

    <a id="voice-email-btn">Voice Email</a> <br>

    <a id="chat-btn">Start Chat</a> <br>

    <a id="sendfile-btn">Send File</a> <br>


    <!-- ### PLACE HERE YOUR CODE AFTER YOUR HTML CONTENT     -->

    <script type="text/javascript" language="javascript">

         function el(elementId, username, action) {

             document.getElementById(elementId).setAttribute("href", "skype:" + 

              username + "?" + action);

         }


    function buildLinkRefs() {

        var username = document.getElementById("username").value;


        el("call-btn", username, "call");

        el("add-to-contacts-btn", username, "add");

        el("view-profile-btn", username, "userinfo");

        el("voice-email-btn", username, "voicemail");

        el("chat-btn", username, "chat");

        el("sendfile-btn", username, "sendfile");

      }


     document.getElementById("username").addEventListener("change", function () {

         buildLinkRefs();

     }, false);


    buildLinkRefs(); 

</script>


查看完整回答
反對 回復 2022-09-16
  • 2 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號