在 html 中添加属性时出错。我该如何修复它?

我试图使用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>


MM们
浏览 167回答 2
2回答

翻过高山走不出你

您使用了错误的事件侦听器。文本输入应使用“键控”,而不是“更改”document.getElementById('username').addEventListener('keyup', buildLinkRefs );因此,使用您的代码,它应该如下所示:function el(elementId, username, action) {&nbsp; &nbsp; document.getElementById(elementId).setAttribute("href", "skype:" + username + "?" + action);}function buildLinkRefs() {&nbsp; &nbsp; var username = document.getElementById("username").value;&nbsp; &nbsp; el("call-btn", username, "call");&nbsp; &nbsp; el("add-to-contacts-btn", username, "add");&nbsp; &nbsp; el("view-profile-btn", username, "userinfo");&nbsp; &nbsp; el("voice-email-btn", username, "voicemail");&nbsp; &nbsp; el("chat-btn", username, "chat");&nbsp; &nbsp; el("sendfile-btn", username, "sendfile");}document.getElementById("username").addEventListener("keyup", buildLinkRefs );buildLinkRefs();&nbsp;&nbsp;<input type="text" id="username" value="echo123"/><br>&nbsp; &nbsp; <br>&nbsp; &nbsp; <a id="call-btn">Call</a> <br>&nbsp; &nbsp; <a id="add-to-contacts-btn">Add to contacts</a> <br>&nbsp; &nbsp; <a id="view-profile-btn">View User Profile</a> <br>&nbsp; &nbsp; <a id="voice-email-btn">Voice Email</a> <br>&nbsp; &nbsp; <a id="chat-btn">Start Chat</a> <br>&nbsp; &nbsp; <a id="sendfile-btn">Send File</a> <br>

青春有我

问题是,您的函数在加载整个页面之前被触发,因此它看不到该元素,因为该元素不可用 。因此,有很多方法可以解决它 。只需将代码放在html元素之后或下方,如下所示:username<html>&nbsp;<head>&nbsp; &nbsp; <title>the title</title>&nbsp;</head>&nbsp; &nbsp; &nbsp;<body>&nbsp; &nbsp; &nbsp; &nbsp;<input type="text" id="username" value="echo123"/><br>&nbsp; &nbsp;<br>&nbsp; &nbsp; <a id="call-btn">Call</a> <br>&nbsp; &nbsp; <a id="add-to-contacts-btn">Add to contacts</a> <br>&nbsp; &nbsp; <a id="view-profile-btn">View User Profile</a> <br>&nbsp; &nbsp; <a id="voice-email-btn">Voice Email</a> <br>&nbsp; &nbsp; <a id="chat-btn">Start Chat</a> <br>&nbsp; &nbsp; <a id="sendfile-btn">Send File</a> <br>&nbsp; &nbsp; <!-- ### PLACE HERE YOUR CODE AFTER YOUR HTML CONTENT&nbsp; &nbsp; &nbsp;-->&nbsp; &nbsp; <script type="text/javascript" language="javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function el(elementId, username, action) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;document.getElementById(elementId).setAttribute("href", "skype:" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; username + "?" + action);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; function buildLinkRefs() {&nbsp; &nbsp; &nbsp; &nbsp; var username = document.getElementById("username").value;&nbsp; &nbsp; &nbsp; &nbsp; el("call-btn", username, "call");&nbsp; &nbsp; &nbsp; &nbsp; el("add-to-contacts-btn", username, "add");&nbsp; &nbsp; &nbsp; &nbsp; el("view-profile-btn", username, "userinfo");&nbsp; &nbsp; &nbsp; &nbsp; el("voice-email-btn", username, "voicemail");&nbsp; &nbsp; &nbsp; &nbsp; el("chat-btn", username, "chat");&nbsp; &nbsp; &nbsp; &nbsp; el("sendfile-btn", username, "sendfile");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;document.getElementById("username").addEventListener("change", function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;buildLinkRefs();&nbsp; &nbsp; &nbsp;}, false);&nbsp; &nbsp; buildLinkRefs();&nbsp;</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript