javascript - 如何从捕获的 innerHTML 值构造 JavaScript 中的链接?
问题描述
假设我有
<a href="#" id="someid">findme</a> (innertext can be whatever other than findme)
当我点击“findme”时,我想重定向到页面“/?q=findme”。(应该在 /?q= 之后插入值以创建新链接,并且页面应该重定向到该链接。)如何使用 javascript 做到这一点?
解决方案
获取链接的内部文本并修改 href 属性以指向您的首选位置。
const linkElement = document.getElementById('someid'); //get your link element by id
const innerValue = linkElement.innerText // get inner text value
linkElement.href = '/?q=' + innerValue // set href attribute
<a href="#" id="someid">findme</a>
推荐阅读
- android - 如何在包含所有曲目的 mpd 文件上的自定义演员应用程序中选择音频/字幕?
- reactjs - React + redux,组件通过 redux-thunk 获取数据并订阅它,但 props 在更改时不会更新
- c# - 如何从 SQLite 中删除数据?
- mongodb - 如何加入两个 MongoDB 集合 + 组?
- emq - emqtt在哪里存储数据
- python - 当我在一个函数中声明多个连接时,如何确保连接关闭?
- javascript - 如何创建一个动态列表,其项目描述仅在单击项目时显示?
- excel - 从活动单元格为每个单元格执行循环
- node.js - 如何删除位于文件夹内的唯一文件(图像)
- pocketsphinx-android - 为什么 Pocketsphinx 唤醒词检测会随着时间的推移变得更糟?