如何使用JS / jQuery在DIV元素上模拟click事件

时间:2022-09-30 21:04:22

http://services.groupes.be/ibrunet/ibrunet.aspx?lg=NL

I am trying to simulate click events on DIV elements with class="x-grid-cell-inner"(with text Ibrunet, Signaletiek..)

我试图用class =“x-grid-cell-inner”模拟DIV元素上的点击事件(带文字Ibrunet,Signaletiek ..)

First I inserted jQuery.

首先我插入了jQuery。

javascript:var s=document.createElement('script');s.setAttribute('src', 'http://code.jquery.com/jquery.js');document.getElementsByTagName('body')[0].appendChild(s);alert("loaded");void(s);

Then I tried this

然后我尝试了这个

var bedragenDivClass = "x-grid-cell-inner ";
var bedragenDivText = "Bedragen";
var divs = document.getElementsByClassName(bedragenDivClass);
for (var i = 0, len = divs.length; i < len; i++) {
    if(divs[i].innerText.localeCompare(bedragenDivText) == 0){
        alert("found");
    };
};

And I've got referrence to this DIV but then I tried several different functions to trigger click event without any success

而且我已经引用了这个DIV,但后来我尝试了几个不同的函数来触发click事件而没有任何成功

.trigger()

.triggerHandler()

.click()

When I open Chrome Dev Tools I can see several handler bounded to that DIV but I dont know how to trigger them

当我打开Chrome Dev Tools时,我可以看到几个处理程序与DIV绑定但我不知道如何触发它们

Unusual thing is that I could simulate click on input elements on right panel

不寻常的是我可以在右侧面板上模拟点击输入元素

var contractTypeInputId = "Cmb_Type_Contrat_Ibrunet_PLus-inputEl";
var contractTypeInput = document.getElementById(contractTypeInputId);
contractTypeInput.click();

Also I could click on elemenets of that input that show after script click it.

此外,我可以单击脚本单击它后显示的输入的elemenet。

Since those DIV's don't have id attribute and I thought I need id to trigger the event I've gave them inside Dev Tools and I could retrieve it after but again no success with triggering onClick event.

由于那些DIV没有id属性,我认为我需要id来触发事件,我已经在Dev Tools中给了它们,我可以在之后检索它但是再次触发onClick事件没有成功。

The strangest thing is when I run something like this:

最奇怪的是当我运行这样的事情时:

$("div").click();

I can see many DIV's beign clicked but those with that class I specified are not affected.

我可以看到很多DIV的beign被点击了,但那些我指定的那个类没有受到影响。

If I can trigger the event as simple as clicking on that DIV why I failed simulating it?

如果我可以触发事件就像点击那个DIV一样简单,为什么我没有模拟它?

1 个解决方案

#1


1  

Using jquery

$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").click(function () {
 alert(1);
});
$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").trigger("click");

#1


1  

Using jquery

$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").click(function () {
 alert(1);
});
$(".-grid-cell-inner:contains(Ibrunet, Signaletiek)").trigger("click");