简体   繁体   English

如何使用javascript在特定按钮下方显示信息

[英]how to display information underneath a specific button using javascript

I have the following piece of code I am working on. 我正在处理以下代码。 My purpose is to be able to grab information about different users from a specific website, display the name and other info and then have a button that when clicked, prints more information. 我的目的是能够从特定网站获取有关不同用户的信息,显示名称和其他信息,然后有一个按钮,单击该按钮即可打印更多信息。 I am able to get the information and display the name and picture, but when I click the button the information is displayed at the top of the page, not under the specific button that was clicked. 我可以获取信息并显示名称和图片,但是当我单击按钮时,信息显示在页面顶部,而不是单击的特定按钮下方。 I want for the information to be display under each user... I am new to Javascript and learning on my own, any help is appreciated! 我希望将信息显示在每个用户下...我是Java语言的新成员,自己学习,对您的帮助非常感谢!

function getUsers(user) {
    var out = "";
    var i;
    for (i = 0; i < user.length; i++) {
        out += '<a href="' + user[i].url + '">' + user[i].login + '</a><br>'+'</br> <img src="'+user[i].avatar_url+
               '" alt="Image" style="width:304px;height:228px"</br></br>'+
               '<button onclick=printRepos("'+user[i].repos_url+'")>Repositories</button></br>'+'<div id="id"></div>';
    }
    document.getElementById("id01").innerHTML = out;
}

Printing Function 打印功能

function printF(array) {
    var out = "";
    var i;
    for (i = 0; i < array.length; i++) {
        out += array[i].id+'</br>';
    }
    document.getElementById("id").innerHTML = out;
}

This works fine. 这很好。 I just made div with dynamic ids and passed it to the function 我只是用动态ID制作了div并将其传递给了函数

function getUsers(user) {
    var out = "";
    var i;
    for (i = 0; i < user.length; i++) {
         out += '<a href="' + user[i].url + '">' + user[i].login + '</a>   <br>'+'</br> <img src="'+user[i].avatar_url+
           '" alt="Image" style="width:304px;height:228px"</br></br>'+
           '<button onclick=printRepos("'+user[i].repos_url+'","'+i+'")>Repositories</button></br>'+'<div id="'+ 'id' + i +'"></div>';
    }

   document.getElementById("id01").innerHTML = out;
}


function printRepos(array, id) {

    var out = "";
    var i;
    for (i = 0; i < array.length; i++) {
        out += array[i].id+'</br>';
    }

    console.log('id' + id);

    document.getElementById('id' + id).innerHTML = out;


}

Add the "this" keyword as a parameter to your onclicks, to pass in the button that was clicked: 将“ this”关键字作为参数添加到您的onclicks中,以传递被单击的按钮:

<button onclick=printRepos(this,"'+user[i].repos_url+'")>Repositories</button>

Then locate the next div after that button in your event handler: 然后在事件处理程序中该按钮之后找到下一个div:

function printF(btn, array) {
    var out = "";
    var i;
    for (i = 0; i < array.length; i++) {
        out += array[i].id+'</br>';
    }
    // find the div
    var d = btn;  // start with the button
    while (d.tagName != "DIV") d = d.nextSibling; // look for the next div
    d.innerHTML = out;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用基于使用JavaScript单击的特定按钮的数组中的信息替换div上的信息? - How can i replace information on a div with information from an array based on specific button being clicked using JavaScript? 如何使用 JavaScript 显示用户输入的信息? - How to display user entered information using JavaScript? 使用来自特定按钮的信息显示模态 - Display modal with information from a specific button 如何使用javascript仅为特定客户的最后插入行显示编辑按钮 - How to display Edit Button only for last insertion Row of specific customer using javascript 当我使用javascript单击按钮时,如何在另一页上显示特定图像? - How to make a specific image display on another page when I click on a button using javascript? 如何使用javascript隐藏特定网页上的按钮? - How to hide a button on a specific webpage using javascript? 如何使用javascript登录linkedin并显示个人资料信息 - How to Login with linkedin using javascript and display profile information 使用 Javascript 获取并显示 API 信息 - Fetch and display API information using Javascript HTML、JavaScript。 如何在同一个弹窗按钮上显示不同的信息 - HTML, JavaScript. How to display different information on the same popup window button 如何使用jQuery Bootstrap在按钮单击上显示特定的div - how to display specific div on button click using jquery bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM