javascript函数只适用于第一个元素
javascript function only works with first element
我有一个xsl文件,我可以让它在点击函数中显示标题,但我只能显示第一个标题,其他标题保持隐藏,无论哪个按钮调用函数,第一个标题都会显示。该功能在按下按钮后也不会隐藏按钮,所以该功能和特定行一定有问题吗?此外,我将用internet explorer打开这个。。
<script language="javascript">
function ShowTitle(title, btn)
{
document.getElementById("title").style.display = 'inline'
title.style.display = 'inline'
btn.style.display = 'none'
}
</script>
<body>
<xsl:for-each select="/questions/question/movie[position() >1]">
<div style="background-color:tan;color:black;">
<span style="font-weight:bold">In What Movie Did They Say:
<xsl:value-of select="quote"/> -
</span>
<input type="button" value="Show title" onclick="ShowTitle('title', this)"></input>
<span id="title" style="display:none">
<xsl:value-of select="title"/></span>
</div>
<div style="margin-bottom:1em;font-size:15pt">
</div>
<br/>
</xsl:for-each>
正如OJay所说,问题是当XSL使用id
生成多个元素时,您正试图通过id
检索元素。
你应该更改你的html 的这一部分
<div style="background-color:tan;color:black;">
<span style="font-weight:bold">In What Movie Did They Say:
<xsl:value-of select="quote"/> -
</span>
<input type="button" value="Show title" onclick="ShowTitle(this)"></input>
<span class="title" style="display:none">
<xsl:value-of select="title"/>
</span>
</div>
然后,您可以按如下方式更新ShowTitle
方法:
function ShowTitle(btn) {
// get the btn's parent element. In this case the div
var btnParentDiv = btn.parentElement;
// use getElementsByClassName to retrieve the title
// getElementsByClassName returns an array of elements.
// In this case this is an array of length == 1
// we're just interested in the first result, so get it immediately
var divTitleElement = btnParentDiv.getElementsByClassName('title')[0];
// Set the selected element to be visible
divTitleElement.style.display = 'inline'
// hide the button as before
btn.style.display = 'none'
}
这个代码可以缩短,但我已经分解了一些步骤来包括解释
相关文章:
- 使用数据上的角度更改设置集合的第一个元素的动画
- 在javascript数组中推送多个值并获取第一个元素
- Angular js将只显示ng repeat中的第一个元素
- 使用+=运算符未定义对象中的第一个元素
- javascript window.addEventListener,覆盖了第一个元素的配置
- 如何访问 JSON 对象数组的第一个元素
- 第一个元素和最后一个元素之间的连续循环
- 要插入二叉树的第一个元素,请将其放在左边还是右边
- 向下滚动时覆盖页面的第一个元素或块
- 数组未发送所有元素,只获取第一个元素
- 创建每个数组的第一个元素的数组
- 只显示ng重复的第一个元素
- CKEditor:以小部件作为第一个元素,选择所有在Chrome中不工作的
- 从Web视图上显示的页面部分获取第一个元素
- 有没有更好的方法来访问用JQuery选择器选择的第一个元素
- JQuery验证仅适用于提交的表单中的第一个元素.[打算验证所有内容]
- jQuery导航菜单没有't更新第一个元素
- jQuery如何删除第一个元素前的逗号
- 如何获取数组第一个元素的范围
- 使用 Dart 时,我正在创建一个点击事件的 DOM 类.它只拾取第一个元素,我如何让它在所有元素上发射