如何滚动到iframe中的特定div?

时间:2017-03-21 15:17:21

标签: javascript html angularjs iframe

我有一个带有iframe的div,就像这样:

<div>
    <iframe ng-src="{{somesourceurl}}"></iframe>
</div>

由iframe加载的源代码具有带有唯一标识的div标签的html,如下所示:

<div id="n_1">
    <div id="n_2" />
    <div id="n_3" />
    <div id="n_4">
        <div id="n_5" />
    </div>
</div>

我还有一堆按钮,对应于这些唯一标记的div中的每一个,如下所示:

<button onclick="goToN1()">Go To n1</button>
<button onclick="goToN2()">Go To n2</button>
<button onclick="goToN3()">Go To n3</button>
<button onclick="goToN4()">Go To n4</button>
<button onclick="goToN5()">Go To n5</button>

我为每个按钮设置了onclick功能,以便它们滚动到iframe中的适当位置?

1 个答案:

答案 0 :(得分:1)

我认为您可以更改src的{​​{1}}以使浏览器滚动到包含iframe的元素:

HTML 1

id

HTML 2

<div>
    <iframe id="my-iframe" ng-src="{{somesourceurl}}"></iframe>
</div>

的JavaScript

<button onclick="goToId('n_1')">Go To n1</button>
<button onclick="goToId('n_2')">Go To n2</button>
<button onclick="goToId('n_3')">Go To n3</button>
<button onclick="goToId('n_4')">Go To n4</button>
<button onclick="goToId('n_5')">Go To n5</button>