[英]Click on li with specific “rel” : Pure javascript, no jquery
I have to do a fully JavaScript (jQuery forbidden) script. 我必须做一个完整的JavaScript(禁止jQuery)脚本。 I only can use the 'click()' function.
我只能使用“ click()”函数。
I have to simulate a click on a "li" which has a random attribute for a sneakers website. 我必须模拟对“ li”的点击,该“ li”具有运动鞋网站的随机属性。
For showing you : 为了向您展示:
I show you the "code" of the html big menu. 我向您展示html大菜单的“代码”。 On this article, "LZAE31" is the ID product.
在本文上,“ LZAE31”是ID产品。
<ul class="theUlClass" style="display:none">
<li class="theLiClass" rel="LZAE31:40">EU 40</li>
<li class="theLiClass" rel="LZAE31:41">EU 41</li>
<li class="theLiClass" rel="LZAE31:42">EU 42</li>
<li class="theLiClass" rel="LZAE31:43">EU 43</li>
</ul>
Imagine that the user has already buy something on the website, and his size is 42 EU. 想象一下,用户已经在网站上购买了商品,他的身高是42欧盟。 The script is 90% ok (the part that the user has already buy something), but now i need to "simulate" a click.
该脚本可以90%正常运行(用户已经购买了一部分商品),但是现在我需要“模拟”点击。
For exemple, the end of the script works and it is : 例如,脚本的结尾有效,它是:
document.getElementsByClassName("addtocart")[0].click();
So I really need you, to understand how can i click on the "rel="RANDOM:42" in the li.TheLiClass for exemple... 因此,我真的很需要您,以了解如何在li.TheLiClass中单击“ rel =“ RANDOM:42”。
Try with: 尝试:
First select element: 首先选择元素:
With Selector: 使用选择器:
document.querySelector("[rel='LZAE31:40']")
// try like: console.log(document.querySelector("[rel='LZAE31:40']"));
Or with this function : 或具有此功能:
function specificRel( rel )
{
var items = document.getElementsByTagName('li');
for (var i=0; i<items.length; i++) {
if (items[i].getAttribute("rel") == rel) {
return items[i];
}
}
}
// try like: console.log(specificRel("LZAE31:40"));
And two, click : 然后点击两次:
With selector: 使用选择器:
document.querySelector("[rel='LZAE31:40']").click();
Or with the function: 或带有功能:
specificRel("LZAE31:40").click();
It's a tad bit unclear what you're asking, but I'll give it a shot. 有点不清楚您要问什么,但我会给您一个机会。 To get the LI element with
rel=LZAE31:43
is trivial. 用
rel=LZAE31:43
获得LI元素很简单。
for( var a= document.getElementsByTagName('li'),i= 0; i < a.length; ++i )
if( a[i].getAttribute('rel') == 'LZAE31:43' )
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.