【发布时间】:2020-06-04 05:47:39
【问题描述】:
我正在尝试使用Skype 与Html 和javascript 进行聊天。 这里的问题是我无法将属性 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>
【问题讨论】:
-
你遇到了什么错误?
-
<a>标签上的href 不需要setAttribute,只需调用document.getElementById(elementId).href = ... -
您的示例仅在输入用户名时无法加载,在您的事件侦听器中将
change更改为input或keyup。 -
抱歉没把问题说清楚。所以,我的问题是我不能用我的脚本来引用 ,我不明白为什么。
-
根本没有修复@asdru
标签: javascript html href