所以这只是一个小型的个人项目,我正在使用.net中的awesomium。所以在awesomium中,我打开了这个浏览器,并且我想点击这个包含此代码的按钮。
<a class="buttonright" > Bump </a>
但是考虑到它是一个课程,而不是一个按钮,我很难找到一个方法来点击&#34;它。我的计划是使用awesomium中的javascript点击它,但也许我是从错误的方向接近这个?
由于
答案 0 :(得分:2)
<强> 更新 强>
经过大量的评论(来回),我设置了一个小提琴,这个代码的工作版本(这里的代码也适用,但需要一些调试)。小提琴中的eventTrigger
函数已被删除所有注释,但我已添加了此函数的示例用法,其中慷慨地散布了注释。
浏览它,分叉,玩耍并熟悉那里使用的代码和概念。玩得开心:
如果通过&#34;找到一种方法来点击它&#34; 你的意思是:如何以编程方式点击这个锚元素,那么这就是你可以使用的:
这是一个X浏览器,略显冗长而全面的方法:
var eventTrigger = function(node, event)
{
var e, eClass,
doc = node.ownerDocument || (node.nodeType === (document.DOCUMENT_NODE || 9) ? node : document);
//after checking John Resig's Pro JavaScript Techniques
//the statement above is best written with an explicit 9
//Given the fact that IE doesn't do document.<NODE_CONSTANT>:
//doc = node.ownerDocument || (node.nodeType === 9 ? node : document);
if (node.dispatchEvent)
{//dispatchEvent method is present, we have an OK browser
if (event === 'click' || event.indexOf('mouse') >= 0)
eClass = 'MouseEvents';//clik, mouseup & mousedown are MouseEvents
else
eClass = 'HTMLEvents';//change, focus, blur... => HTMLEvents
//now create an event object of the corresponding class
e = doc.createEvent(eClass);
//initialize it, if it's a change event, don't let it bubble
//change events don't bubble in IE<9, but most browsers do
//e.initEvent(event, true, true); would be valid, though not standard
e.initEvent(event, !(event === 'change'), true);
//optional, non-standard -> a flag for internal use in your code
e.synthetic = true;//mark event as synthetic
//dispatch event to given node
node.dispatchEvent(e, true);
//return here, to avoid else branch
return true;
}
if (node.fireEvent)
{//old IE's use fireEvent method, its API is simpler, and less powerful
//a standard event, IE events do not contain event-specific details
e = doc.createEventObject();
//same as before: optional, non-standard (but then IE never was :-P)
e.synthetic = true;
//~same as dispatchEvent, but event name preceded by "on"
node.fireEvent('on' + event, e);
return true;//end IE
}
//last-resort fallback -> trigger any directly bound handler manually
//alternatively throw Error!
event = 'on' + event;
//use bracket notation, to use event's value, and invoke
return node[event]();//invoke "onclick"
};
在您的情况下,您可以通过查询该特定元素的DOM来使用此功能,如下所示:
var elem = document.querySelector('.buttonright');//IE8 and up, will only select 1 element
//document.querySelectorAll('.buttonright'); returns a nodelist (array-like object) with all elements that have this class
eventTrigger(elem, 'click');
这应该具有单击锚元素
的效果如果您正在寻找一种方法来处理此元素上的点击事件(具有buttonright
类的锚点),那么您只需要一个简单的事件监听器:
document.body.addEventListener('click', function(e)
{
e = e || window.event;
var target = e.target || e.srcElement;
if (target.tagName.toLowerCase() === 'a' && target.className.match(/\bbuttonright\b/))
{//clicked element was a link, with the buttonright class
alert('You clicked a button/link thingy');
}
}, false);
这是最干净的做事方式(一个事件监听器处理所有点击事件)。当然,您也可以将处理程序绑定到特定元素:
var buttons = document.querySelectorAll('.buttonright'),
handler = function(e)
{
alert('Clicked!');
};
for (var i=0;i<buttons.length;++i)
{
buttons[i].addEventListener('click',handler, false);
}
答案 1 :(得分:0)
根据您希望如何处理事件,您可以选择多条道路。
最简单的是:
<script type="text/javascript">
function buttonRight_onclick(event, sender)
{
alert("HEY YOU CLICKED ME!");
}
</script>
<a class="buttonright" click="buttonRight_onclick(event, this)">
如果你使用像jQuery这样的框架,你可以这样做:
<script type="text/javascript">
$(document).ready(function() {
$(".buttonright").on("click", function(event) {
alert("HEY YOU CLICKED ME!");
});
});
</script>
<a class="buttonright" >Bump</a>
<a class="buttonright" >Also bump</a>
答案 2 :(得分:0)
<script type="text/javascript">
function Button_onclick(event, sender)
{
alert("Button Clicked!");
}
</script>
<a class="Button" click="Button_onclick(event, this)">