Javascript 按钮ID为getElementById

Javascript 按钮ID为getElementById,javascript,getelementbyid,Javascript,Getelementbyid,我可以在getElementById中获取触发函数的按钮ID值吗 我正在尝试编写一个通用函数,其中getElementById应该与按钮ID相同,因此使用具有不同按钮ID的按钮触发函数会导致不同的getElementById HTML: 所以我只能有一个通用函数,我可以用它从不同的文本区域复制 <!doctype html> <html> <head> <script> "use strict"; window.addEvent

我可以在getElementById中获取触发函数的按钮ID值吗

我正在尝试编写一个通用函数,其中getElementById应该与按钮ID相同,因此使用具有不同按钮ID的按钮触发函数会导致不同的getElementById

HTML:

所以我只能有一个通用函数,我可以用它从不同的文本区域复制

<!doctype html>
<html>
<head>
<script>
"use strict";
window.addEventListener('load', onLoaded, false);

// called when the window has finished loading
// - attaches a click handler to all buttons.
//   buttons are selected with the css selector 'button'
//   'button.someClass' would select all 'button' elements
//   that had the class 'someClass'
function onLoaded(evt)
{
    let buttons = document.querySelectorAll('button');

// this code just does the same thing as the line below
//  buttons.forEach( 
//          function(btn)
//          { 
//              btn.addEventListener('click', onCopyBtnClick, false); 
//          } 
//      );
        
    buttons.forEach( btn => btn.addEventListener('click', onCopyBtnClick, false) );
}

function onCopyBtnClick(evt)
{
    // get reference to btn that fired the event
    let btn = this;

    // the textarea may not be the element immediately following
    // the button. In the code below, there's actually a 
    // text-node between the two walk through the list of 
    // siblings until we get to the first text-area after the button.
    let tmp = btn.nextSibling;
    while (tmp.nodeName != 'TEXTAREA')
        tmp = tmp.nextSibling;
    let targetTextarea = tmp;

    // select and copy the text
    targetTextarea.select();
    document.execCommand('copy');
}
</script>
</head>
<body>
    <button>COPY</button>
    <textarea rows='1' cols='25'>Test123 </textarea>
    
    <button>COPY</button>
    <textarea rows='1' cols='25'>Test456 </textarea>
</body>
</html>
复制 测试123 复制
Test456基于您共享的代码,解决方案的一种方法如下:

//定义函数以及表示 //已单击的元素: 功能f001_元素{ //这里我们使用HTMLElement.id属性来检索 //所单击对象的“id”属性/属性的值 //元素并使用parseInt-以及基数10- //要将该id转换为数字,请执行以下操作: var integer=parseInt_elem.id,10, //这里我们使用document.getElementById来查找 //元素的id等于 //已递增1: copyText=document.getElementById++整数; //我们从标识的值中选择输入的值 //要素: copyText.select; //并执行“复制”操作以分配内容 //到剪贴板: 文件副本; }
在现代浏览器中,除了IE,什么都可以 您可以使用W3C标准自定义元素创建自己的HTML标记:

你好,世界! 你好,另一个世界! customElements.definemy-textarea,类扩展HtmleElement{ connectedCallback{ setTimeout=>{//必需,因为只有在DOM就绪后才能读取innerHTML 设textarea=document.createElementtextarea; let button=document.createElementbutton; button.innerHTML=复制我; button.id=foo;//不需要id,只需显示重复id的作用 button.onclick=evt=>{//inline事件很好,我们没有附加更多 textarea.select;//在范围内,因此无需搜索textarea 文件副本; console.logbutton.id,窗口[button.id];//查看FireFox和Chrome中的差异 console.logdocument.querySelectorAllfoo; } textarea.innerHTML=this.innerHTML; this.innerHTML=; this.appendtextarea,button;//了解appendChild的区别 } } }
这听起来好像有多个元素具有相同的id,这是无效的html:id是文档中某个元素的唯一标识符。你能分享你的相关代码吗,包括html和JavaScript?你的意思是我不能和button有相同的id,也不能和textarea id有相同的id?这是不同的元素…是的。id在文档中必须是唯一的。如有必要,可以使用“自定义”链接元素或类名。但是,由于您没有共享代码,我们无法提供任何帮助,并且很可能会解决此问题,因为共享代码是一项要求。您是否意识到如此处所示分配的事件处理程序与通过使用.addEventListener分配的事件处理程序之间的差异?在您显示的html中,textarea是包含按钮和textarea的元素的下一个子元素。当在通过.addEventListener连接的处理程序的代码中时,此保留字指向按钮。您可以请求任何其他元素的下一个同级。这个.nextSibling就可以了。我很快会写一个答案。您正在使用onclick事件调用函数,click事件有一个属性告诉您单击的对象,除非您需要在函数中硬编码按钮名称,否则您最好使用event.target,请参阅感谢您的回答和解释,对于像我这样的初学者非常有用:
    function f001() {
    var copyText = document.getElementById("triggering button id + 1");
    copyText.select();
    document.execCommand("copy");
}
<!doctype html>
<html>
<head>
<script>
"use strict";
window.addEventListener('load', onLoaded, false);

// called when the window has finished loading
// - attaches a click handler to all buttons.
//   buttons are selected with the css selector 'button'
//   'button.someClass' would select all 'button' elements
//   that had the class 'someClass'
function onLoaded(evt)
{
    let buttons = document.querySelectorAll('button');

// this code just does the same thing as the line below
//  buttons.forEach( 
//          function(btn)
//          { 
//              btn.addEventListener('click', onCopyBtnClick, false); 
//          } 
//      );
        
    buttons.forEach( btn => btn.addEventListener('click', onCopyBtnClick, false) );
}

function onCopyBtnClick(evt)
{
    // get reference to btn that fired the event
    let btn = this;

    // the textarea may not be the element immediately following
    // the button. In the code below, there's actually a 
    // text-node between the two walk through the list of 
    // siblings until we get to the first text-area after the button.
    let tmp = btn.nextSibling;
    while (tmp.nodeName != 'TEXTAREA')
        tmp = tmp.nextSibling;
    let targetTextarea = tmp;

    // select and copy the text
    targetTextarea.select();
    document.execCommand('copy');
}
</script>
</head>
<body>
    <button>COPY</button>
    <textarea rows='1' cols='25'>Test123 </textarea>
    
    <button>COPY</button>
    <textarea rows='1' cols='25'>Test456 </textarea>
</body>
</html>