java - 如何在不单击href链接的情况下引用div id?
问题描述
我正在尝试在 python 中使用 streamlit 呈现网页,我想自动滚动到页面的特定部分。我不是 html 或 java 方面的专家,所以我很感激带有示例的解决方案。
所以我在页面底部有一个 div,例如,
st.markdown("<div id='linkto_bot'></div>", unsafe_allow_html=True)
我在顶部有这个 html 代码,
st.markdown("<a href='#linkto_bot'>Link to bottom</a>", unsafe_allow_html=True)
如图所示,它呈现一个链接,
单击链接后,我将滚动到div
添加 is 的部分。
是否有可能被重定向到div
而无需点击链接?
我尝试了以下但徒劳无功:
st.markdown("a href='#linkto_bot', unsafe_allow_html=True)
在网上某处看到这个,但没有工作。- 使用JavaScript,
html_string = '''
<script language="javascript">
location.href = "#";
location.href = "#linkto_bot";
</script>
'''
components.html(html_string)
这也不起作用。
解决方案
你可以使用javascript函数scrollIntoView()
document.getElementById('linkto_bot').scrollIntoView();
推荐阅读
- apache-flink - Apache 光束:状态规范中的 TTL
- flutter - 如何在 Flutter 中格式化 DateTime?删除 DateTime 中的毫秒数?
- tensorflow - tf.keras.models.load_model raise TypeError: 'module' object is not callable
- instagram - 依靠 ?__a=1 进行 instagram 提要安全吗?
- r - 我可以结合使用 group_by 函数应用一致的样本编号吗?
- java - 如果 xml 文档包含名称apcecontext,XPathFactoryImpl 无法识别根节点
- mysql - 无法使用 phpMyAdmin 导入数据库
- c++ - 是否可以使用指向对象的 void* 指针调用方法指针
- python - 找到总和最大的 idx
- primeng - 如何在 PrimeNg 的下拉列表中换行?