首页 > 解决方案 > 试图通过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 页面上对客户端进行虚假点击。

标签: javascriptjqueryevent-handlingdom-eventsclient-side

解决方案


问题是由于itemidgetElementsByClassName.

您应该getElementById()与 id 值(减去#前缀)或querySelector()(带#前缀)一起使用。以下任何一个示例都适合您:

var item = document.getElementById('ManageSuppliersNew');
item.click();
var item = document.querySelector('#ManageSuppliersNew');
item.click();

推荐阅读