Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问通过JavaScript动态创建的按钮的innerHTML_Javascript_Html - Fatal编程技术网

如何访问通过JavaScript动态创建的按钮的innerHTML

如何访问通过JavaScript动态创建的按钮的innerHTML,javascript,html,Javascript,Html,这是一个用于创建动态按钮的JavaScript函数。我想访问按钮的innerHTML function mynumber() { var i,j = 1, num = 0; # i used for making three buttons in row and j used for repeat same process no of time and num is int text on button do { for (i = 1; i <= 3;

这是一个用于创建动态按钮的JavaScript函数。我想访问按钮的innerHTML

function mynumber() {   
    var i,j = 1, num = 0; # i used for making three buttons in row and j used for repeat same process no of time and num is int text on button
    do {
        for (i = 1; i <= 3; i++) {       
            var btn = document.createElement("BUTTON"); # create button using javascript
            var txt = document.createTextNode(num++); # creat text on button
            btn.appendChild(txt); # attached text on button
            btn.id = ('obj'+ k++) ;  
            document.getElementById("btnsize").appendChild(btn); # atache button with text in div 
        } 
        var next = document.createElement("BR");
        document.getElementById("btnsize").appendChild(next);
        j++;
    } 
    while(j<4)          
}

("btn").click(function()
                     {
                       var val = document.getElementById(this.id).innerHTML;
                           document.getElementById("demo").value = val;
                                    }) 
函数mynumber(){
变量i,j=1,num=0;#i用于在行中生成三个按钮,j用于重复相同的过程时间编号,num是按钮上的int文本
做{

对于(i=1;i在脚本代码中使用下面的脚本,其中“100”是变量,您可以给出任何值,但这将是一个数字;)


您可以将css类应用于按钮,如中所示

在样式表中或html中的
标记之间声明样式,如下所示

在创建button元素的代码下方添加以下行

btn.className = "aClassName";

首先,我将您的
-创建脚本更改为以下内容:

function mynumber () {
    // a reference to the element to which we want
    // to add the newly-created <button> elements:
    var appendTo = document.getElementById('btnsize'),

        // creating a document fragment to contain
        // the elements to be added; to prevent the
        // document from redrawing every time an
        // element is added:
        fragment = document.createDocumentFragment(),

        // creating a <button> element:
        button = document.createElement('button'),

        // creating a <br> element:
        br = document.createElement('br'),

        // initialising an empty variable, for
        // use within the loop:
        clone;

    // you want nine buttons, so use one loop
    // that will run nine times:        
    for (var i = 0; i < 9; i++) {

        // using the empty variable to
        // hold the cloned <button> element:
        clone = button.cloneNode();

        // setting the text of the <button>
        // to the current index/iteration
        // (i) of the loop:
        clone.textContent = i;

        // appending the cloned <button>
        // to the document fragment:
        fragment.appendChild(clone);

        // we want rows of three, but
        // JavaScript is zero-based, so
        // we add 1 to i, and check the
        // remainder of the division by
        // 3; if it's zero the number is
        // evenly-divisible by 3 therefore
        // it's time to add a <br>:
        if ((i+1)%3 === 0) {

            // appendin a cloned <br> element:
            fragment.appendChild(br.cloneNode())
        }
    }

    // appending the fragment to the
    // container element:
    appendTo.appendChild(fragment);
}

// calling the function:
mynumber();
函数mynumber(){
//对所需元素的引用
//要添加新创建的元素,请执行以下操作:
var appendTo=document.getElementById('btnsize'),
//创建要包含的文档片段
//要添加的元素;以防止
//每次重新绘制文档时
//添加了以下元素:
fragment=document.createDocumentFragment(),
//创建元素:
button=document.createElement('button'),
//创建
元素: br=document.createElement('br'), //初始化空变量,例如 //在循环中使用: 克隆 //你需要九个按钮,所以使用一个循环 //这将运行九次: 对于(变量i=0;i<9;i++){ //使用空变量 //保留克隆的元素: clone=button.cloneNode(); //设置 //到当前索引/迭代 //(i)环路的长度: clone.textContent=i; //附加克隆的 //到文档片段: 片段。附加子(克隆); //我们想要三排,但是 //JavaScript是基于零的,所以 //我们将1添加到i,并检查 //除法的剩余部分 //3;如果为零,则数字为 //因此可以被3整除 //是时候添加一个
: 如果((i+1)%3==0){ //在克隆的
元素中追加: fragment.appendChild(br.cloneNode()) } } //将片段附加到 //容器元素: appendTo.appendChild(片段); } //调用函数: mynumber();
函数mynumber(){
var appendTo=document.getElementById('btnsize'),
fragment=document.createDocumentFragment(),
button=document.createElement('button'),
br=document.createElement('br'),
克隆
对于(变量i=0;i<9;i++){
clone=button.cloneNode();
clone.textContent=i;
片段。附加子(克隆);
如果((i+1)%3==0){
fragment.appendChild(br.cloneNode())
}
}
appendTo.appendChild(片段);
}
mynumber();

您熟悉css和jquery吗?您可以调用btn.className=“aClassName”,其中aClassName是css样式,您可以指定宽度或应应用于按钮的任何其他样式属性。
btn.className = "aClassName";
function mynumber () {
    // a reference to the element to which we want
    // to add the newly-created <button> elements:
    var appendTo = document.getElementById('btnsize'),

        // creating a document fragment to contain
        // the elements to be added; to prevent the
        // document from redrawing every time an
        // element is added:
        fragment = document.createDocumentFragment(),

        // creating a <button> element:
        button = document.createElement('button'),

        // creating a <br> element:
        br = document.createElement('br'),

        // initialising an empty variable, for
        // use within the loop:
        clone;

    // you want nine buttons, so use one loop
    // that will run nine times:        
    for (var i = 0; i < 9; i++) {

        // using the empty variable to
        // hold the cloned <button> element:
        clone = button.cloneNode();

        // setting the text of the <button>
        // to the current index/iteration
        // (i) of the loop:
        clone.textContent = i;

        // appending the cloned <button>
        // to the document fragment:
        fragment.appendChild(clone);

        // we want rows of three, but
        // JavaScript is zero-based, so
        // we add 1 to i, and check the
        // remainder of the division by
        // 3; if it's zero the number is
        // evenly-divisible by 3 therefore
        // it's time to add a <br>:
        if ((i+1)%3 === 0) {

            // appendin a cloned <br> element:
            fragment.appendChild(br.cloneNode())
        }
    }

    // appending the fragment to the
    // container element:
    appendTo.appendChild(fragment);
}

// calling the function:
mynumber();