[英]How to access outer innerHTML of some nested div in javascript function
To keep it simple, I have below html code: 为简单起见,我有以下html代码:
<div class = "customers" data-popover="true" data-html=true
data-content="
<table>
<c:forEach items="${element.first}" var="btnVal" varStatus="loop">
<c:if test="${not loop.first}">
<tr> <button onclick='setTestname(innerHTML)' class='btn btn-primary'> ${btnVal}</button> </tr>
<tr> </tr>
</c:if>
</c:forEach>
</table>
">
<br>${element.first.get(0)}
</div>
Here I am trying to use the value of the clicked button using below javascript function: 在这里,我尝试通过以下javascript函数使用clicked按钮的值:
function setTestname(test){
test = test.toString().trim();
document.getElementById('hiddenCostomer').value=test;
setLogfilename(test);
document.getElementById('form1').submit();
}
Above code works fine, but I am not sure abt how to access value of div
as well. 上面的代码可以正常工作,但是我不确定如何也可以访问
div
值。 I want to pass two values to setTestname
我想将两个值传递给
setTestname
${btnVal}
${element.first.get(0)}
How to get value of ${element.first.get(0)}
如何获取
${element.first.get(0)}
EDIT : I want the exact value of div
on which the button is clicked , not the entire DOM. 编辑:我想要单击按钮的
div
的确切值,而不是整个DOM。
You should add the value to a data attribute and then get it, something like this: 您应该将值添加到数据属性,然后获取它,如下所示:
HTML: HTML:
<div id="myid" data-mydata="${element.first.get(0)}"></div>
JS: JS:
document.getElementById("myid").getAttribute("data-mydata");
You can pass the class name of the div you want to fetch contents of. 您可以传递要获取其内容的div的类名。
setTestName(innerHTML, 'customers')
Then use customers as: 然后将客户用作:
document.getElementsByClassName('customers')
which will return the DOM. 这将返回DOM。
<c:set var="BtnIndex" value="${0}"/>
then I set 然后我设定
<tr> <button onclick='setTestname(innerHTML,${BtnIndex + 0})' class='btn btn-primary' > ${btnVal} ${BtnIndex + 0}</button> </tr>
I set one counter and assigned each div a unique number. 我设置一个计数器,并为每个div分配一个唯一的编号。 Later , i fetched the value from array of class
customers
from that unique counter as index 后来,我从该唯一计数器中从类
customers
数组中获取了价值作为索引
function setTestname(test,btnIndex){
test = test.toString().trim();
btnIndex = btnIndex.toString();
var elemArray = document.getElementsByClassName('customers');
for(var i = 0; i < elemArray.length; i++){
elemArray[i].id = i;
}
var elem = document.getElementById(elemArray[btnIndex].id);
var mainDir = elem.innerHTML;
}
It worked !!! 有效 !!! Bingo
答对了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.