通过没有硬编码的函数更改子属性的元素值

时间:2016-12-29 19:59:25

标签: javascript html jsp

我有一个jsp页面,其中的表行包含一个按钮来增加或减少整数:

<input type = "button" id="unique id" value="+" onclick="increase(this)

每个按钮都包含一个具有唯一ID的子项,例如:

<div id="unique div id" align="center">2</div>

该函数包含以下尝试,将<div...>2</div>增加到<div...>3</div>4等,而不将硬编码值传递给函数:

function increase(element){

 //attempt 1            
    document.getElementById((getElementsByTagName('div')[0].id)).innerHTML++;
    return false;

  //attempt 2
    //document.getElementById(document.getElementById(element.id).getElementsByTagName('div')[0].id).innerHTML++;
    //return false;
}

JSP body和table sample:

        <tr>
            <td>shirt</td>
            <td>1.11</td>
            <td>
            <input type="button" id="btn1" value="+" onclick="increase(this);" />
            <div id="1" align="center">2</div>
            </td>
        </tr>

        <tr>
            <td>pant</td>
            <td>2.22</td>
            <td>
                <input type="button" id="btn2" value="+" onclick="increase(this);" />
            <div id="2" align="center">3</div>
            </td>
        </tr>

enter image description here

0 个答案:

没有答案