如何获得<span>标签里的值 在线等

例如:
<div class="content">

<logic:notEmpty name="answerPage" property="list" scope="request">
<% int i = 0; %>
<logic:iterate id="answer" name="answerPage" property="list" scope="request">
---->>>这里 <span id="span<%=i++ %>">${answer.problemAnswer }</span>
<br />
<div class="answerinfo">
<logic:equal value="1" name="ProblemSubjectForm" property="model.status">
<c:if test="${employee.loginId == answer.createManCode}">
<input name="button1" type="button" class="long_btn" value="修改回答" onclick="modify('span<%=i++ %>')" /> <<-----这是提交给JS的方法
</c:if>
<c:if test="${employee.loginId == ProblemSubjectForm.model.crateManCode}">
<input name="button" type="button" class="long_btn" value="定为最佳答案" onclick="setAnswer(${answer.answerId})" />
</c:if>
</logic:equal>
<div class="userinfo"><span class="gray">回答者:</span> <span class="blue"><c:choose><c:when test="${answer.createManName == '匿名'}">${answer.createManName}</c:when>
<c:otherwise><a href="javascript:showUserInfo('${answer.createManCode}')">${answer.createManName}</a></c:otherwise></c:choose></span>   
<span class="gray"><bean:write name="answer" property="createTime" format="yyyy-MM-dd hh:mm:ss"/></span></div>
</div>
<div class="dotted"></div>
</logic:iterate>
</logic:notEmpty>

</div>
</div>

另外 js里面的方法是 function modify(obj){
"获得SPAN的值"
}
js里面
function modify(obj)
{
alert(obj); ------显示ID
alert(document.getElementById('obj').innerText);----不显示值
}

第1个回答  推荐于2016-03-03
innerText兼容性不好,用innerHTML或者nodeValue
function fetchSpanContent(id,spanIndex){
var parentNode = document.getElementById(id);
if(parentNode.tagName&&parent.tagName!="span"){
var spans = parentNode.getElementsByTagName("span");
if(spans[spanIndex])return spans[spanIndex].nodeValue||spans[spanIndex].nodeValue.innerText;
else {
var contents = [], i;
for(i in spans)contents.push(spans[i].nodeValue||spans[i].innerText);
return contents;
}
}else {
return parentNode.nodeValue||parentNode.innerText;
}
}本回答被提问者采纳