一把情況下模擬點擊一般兩個方面,模擬點擊超級連接事件
firefox的兼容的函數為
對HTMLAnchorElement 加入onclick事件
try {
// create a element so that HTMLAnchorElement is accessible
document.createElement('a');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function') {
if (this.onclick({type: 'click'}) && this.href)
window.open(this.href, this.target? this.target : '_self');
}
else if (this.href)
window.open(this.href, this.target? this.target : '_self');
};
}
catch (e) {
// alert('click method for HTMLAnchorElement couldn\'t be added');
}
下面是具體的應用
try {
// create span element so that HTMLElement is accessible
document.createElement('span');
HTMLElement.prototype.click = function () {
if (typeof this.onclick == 'function')
this.onclick({type: 'click'});
};
}
catch (e) {
// alert('click method for HTMLElement couldn\'t be added');
}
下面是網友的其它相關文章也可以參考下。
最近做東西發現用戶在網頁輸入框里面按回車的行為是不固定的。。。
特別是在網頁有多個表單的時候
于是搜了一把找了一個模擬點擊的js,經測試能在firefox和ie上運行
function doClick(linkId, e){
if(e.keyCode != 13){
return;
}
var fireOnThis = document.getElementById(linkId)
if (document.createEvent)
{
var evObj = document.createEvent('MouseEvents')
evObj.initEvent( 'click', true, false )
fireOnThis.dispatchEvent(evObj)
}
else if (document.createEventObject)
{
fireOnThis.fireEvent('onclick')
}
}
其中e是event,內置對象,linkId是模擬被點擊的對象id
比如
這樣的話就能讓用戶按回車來提交表單了~
opera可以再改一下