簡體   English   中英

使用JS / jQuery選擇子元素

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM