javascript - 试图通过Javascript添加假点击ul li项目
问题描述
我正在尝试添加一个虚假的点击无序列表列表项。
我有一个 ul li 列表项:
<ul>
<li>@Html.ActionLink("Manage Suppliers New", "SupplierPartialViewNew", "Supplier", null, new { @class = "navLinks", id = "ManageSuppliersNew" })</li>
</ul>
我希望具有 id 的列表项的 UlManageSuppliersNew
将自动或自己单击。
我正在尝试使用此代码,但无法这样做:
var item = document.getElementsByClassName("#ManageSuppliersNew");
item.click();
我收到错误:
“JavaScript 运行时错误:对象不支持属性或方法‘点击’”
有人可以告诉我如何在已加载所有列表项的 .cshtml 页面上对客户端进行虚假点击。
解决方案
问题是由于item
您id
在getElementsByClassName
.
您应该getElementById()
与 id 值(减去#
前缀)或querySelector()
(带#
前缀)一起使用。以下任何一个示例都适合您:
var item = document.getElementById('ManageSuppliersNew');
item.click();
var item = document.querySelector('#ManageSuppliersNew');
item.click();
推荐阅读
- wordpress - 将自定义帖子类型添加到 WordPress 主页设置
- pyinstaller - Storage share mode not implemented when packaging a Windows executable using PyInstaller under Wine
- python - 不同时间的不同错误消息“SyntaxError: Unexpected token in JSON at position”在 JSON.parse (
)" - javascript - 如何将 JavaScript 函数引用到 HTML 文件中
- r - 获得加权因子分数
- python - 使用 Python 在 JSON 对象中逐行插入值
- react-native - 使用 route.params 传递图像 React native
- python - ImportError:无法从 'network' 导入名称 'Conv2d' (G:\Saadain\Anaconda\envs\SCALNet\lib\site-packages\network.py)
- spring-boot - 由于加载基于 application-dev.properties 的 url,Spring boot 无法启动
- sql - 消息 156,级别 15,状态 1,第 7 行关键字“BETWEEN”附近的语法不正确