javascript - 如何用JS改变路径数据?
问题描述
我想在鼠标悬停时将路径数据更改为无。在鼠标移出时,我想将值更改为其他值。
我的代码有什么问题?
这是我尝试过的:
HTML:
<button id="buttonjs">
<svg width="10" height="10">
<g fill-rule="evenodd">
<path id="pathjs" d="M0 5h7"></path>
</g>
</svg>
</button>
JS:
document.getElementById("buttonjs").onmouseover = function() {mouseOver()};
document.getElementById("buttonjs").onmouseout = function() {mouseOut()};
function mouseOver() {
document.getElementById("pathjs").getAttribute('d, 0');
}
function mouseOut() {
document.getElementById("pathjs").getAttribute('d, M0 5h7');
}
解决方案
.setAttribute('d', 'M0 5h7')
document.getElementById("buttonjs").onmouseover = function() {
mouseOver()
};
document.getElementById("buttonjs").onmouseout = function() {
mouseOut()
};
function mouseOver() {
document.getElementById("pathjs").setAttribute('d', ' 0');
}
function mouseOut() {
document.getElementById("pathjs").setAttribute('d', 'M0 5h7');
}
Button
<button id="buttonjs">
<svg width="10" height="10">
<g fill-rule="evenodd">
<path id="pathjs" d="M0 5h7"></path>
</g>
</svg>
</button>
推荐阅读
- javascript - Firebase 模拟器可以用于与 React 前端的集成测试吗?
- django - 可以覆盖用户模型以使电子邮件字段成为必需吗?
- reactjs - React-Select,以一种形式传递具有多个选择的道具
- angular - 在 Angular 应用程序的 ng2-smart-table 中使用 tinymce 编辑器?
- c# - 在 Unity 中设置技能
- google-bigquery - 将 BigQuery 传输服务与 CSV 一起使用时,是否可以仅传输某些列?不,所有列?
- git - 我在 VS Code Source Control 窗口中找不到 git 图标,并且隐藏了初始化存储库选项
- mysql - MariaDB - 为什么主键不用于特定表的连接?
- html - 该代码给出了一个解析错误我找不到错误是什么
- c# - 以一定数量的步数在网格中移动 - Unity GameDev