我在 iframe 中有一个表格。我想通过单击 iframe 外部的元素跳转到该表中的特定行。我尝试使用 hash
转到具有指定名称的 anchor 。它似乎仅适用于 <a>
元素但不是 <tr>
如我所愿。请看下面的代码:
<script type="text/javascript">
$(document).ready(function(){
$("#jump").click(function(){
top.myiframe.location.hash = "3";
//top.myiframe.location.hash = "5"; //this works for <a>
});
});
</script>
<p id="jump">Jump</p>
<iframe id="myiframe" src="[my iframe url]" scrolling="no"></iframe>
myiframe.html
包括:
<table>
<tr name="1"><td>1</td></tr>
<tr name="2"><td>2</td></tr>
<tr name="3"><td>3</td></tr>
<tr name="4"><td>4</td></tr>
</table>
<a name="5">5</a>
正如你想象的那样,我可以跳到<a name="5">5</a>
但不是任何<tr>
元素。有什么建议吗?
最佳答案
$(document).ready(function(){
$("#jump").click(function(){
var row = $("#myiframe").contents().find("tr[name=3]"); // Shankar's code
row.scrollIntoView();
});
});
更新:首先让上面的脚本找到一个行对象
没有 jQuery 它也能正常工作
document.getElementsByName("row3")[0].scrollIntoView();
关于javascript - jQuery:如何跳转到 iframe 内表格中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970782/