[英]Selecting child elements with JS/jQuery
我创建了一个单击复制功能,以便用户可以单击一个按钮来复制另一个元素的文本内容。 我已经进行了设置,以便用户可以复制其序列号(该序列号是由简码动态生成的- 在Wordpress中 )。
我在目标容器(包含要复制的文本)为#copyTarget2
且触发按钮为#copyButton2
。 然后,我有了这个运行中的Javascript:
<script>
document.getElementById("copyButton2").addEventListener("click", function() {
copyToClipboardMsg(document.getElementById("copyTarget2"), "msg");
});
document.getElementById("pasteTarget").addEventListener("mousedown", function() {
this.value = "";
});
function copyToClipboardMsg(elem, msgElem) {
var succeed = copyToClipboard(elem);
var msg;
if (!succeed) {
msg = "Copy not supported or blocked. Press Ctrl+c to copy."
} else {
msg = "Text copied to the clipboard."
}
if (typeof msgElem === "string") {
msgElem = document.getElementById(msgElem);
}
msgElem.innerHTML = msg;
setTimeout(function() {
msgElem.innerHTML = "";
}, 2000);
}
function copyToClipboard(elem) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
// can just use the original source element for the selection and copy
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}
if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
target.textContent = "";
}
return succeed;
}
</script>
但是现在,我不得不调整html,以便为没有有效序列号的任何用户动态显示“ NO VALID SERIAL NUMBER”消息。 这意味着包含文本的元素是不同的,并且是#copyTarget2
的子元素。
我需要知道的是:
使用控制台中的以下屏幕截图,谁能告诉我保持复制功能并在#copyTarget2
内选择输入容器的最佳方法?
我已经尝试#copyTarget2 input
, #copyTarget2.input
无效。
请切记,我的JS使用的是GetElementbyID(),因此#copytarget2
input[type="text"]
替换#copytarget2
也不起作用。
将GetElementbyID
更改为querySelector
并尝试使用此选择器
querySelector('input[name="_AFXSERIAL"]')
通过getElementsByTagName
遍历您拥有的元素
document.getElementById('copyTarget2').getElementsByTagName('input')[0].value
或切换到querySelector
document.querySelector('#copyTarget2 input').value
如果您想尝试,可以尝试以下Web API, https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent
这将帮助您删除已编写的整个脚本,并为您提供其他功能,例如剪切和粘贴。
不过请检查兼容性表。
同样,将“#copyTarget2”放入输入中也可以。
DOM会变成
<span>
<strong>
<input id="copyTarget2" />
</strong>
</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.