Javascript 点击href的ue将是一个良好的开端。请注意,即使您不能内联执行onclick=“handleClick()”,您仍然可以使用el.onclick=handleClick。此代码执行不正确。如果我想使用类名而不是Id执行此操作,请以正确的方式指导我。。我试
Javascript 点击href的ue将是一个良好的开端。请注意,即使您不能内联执行onclick=“handleClick()”,您仍然可以使用el.onclick=handleClick。此代码执行不正确。如果我想使用类名而不是Id执行此操作,请以正确的方式指导我。。我试,javascript,google-chrome-extension,onclick,firefox-addon-webextensions,Javascript,Google Chrome Extension,Onclick,Firefox Addon Webextensions,点击href的ue将是一个良好的开端。请注意,即使您不能内联执行onclick=“handleClick()”,您仍然可以使用el.onclick=handleClick。此代码执行不正确。如果我想使用类名而不是Id执行此操作,请以正确的方式指导我。。我试过了,但没有想到如何匿名这个解决方案,使它可以为一个有生成div名称的angular应用程序工作?我无法创建事件侦听器,因为我的ng repeat中的每个项目的按钮名称都必须是唯一的…:(我不确定…是否100%正常工作。txt/html定义已删
点击href的ue将是一个良好的开端。请注意,即使您不能内联执行
onclick=“handleClick()”
,您仍然可以使用el.onclick=handleClick
。此代码执行不正确。如果我想使用类名而不是Id执行此操作,请以正确的方式指导我。。我试过了,但没有想到如何匿名这个解决方案,使它可以为一个有生成div名称的angular应用程序工作?我无法创建事件侦听器,因为我的ng repeat中的每个项目的按钮名称都必须是唯一的…:(我不确定…是否100%正常工作。txt/html定义已删除。
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('link');
// onClick's logic below:
link.addEventListener('click', function() {
hellYeah('xxx');
});
});
<a onclick="handler()">Click this</a> <!-- Bad -->
<a id="click-this">Click this</a> <!-- Fixed -->
// Pure JS:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("click-this").addEventListener("click", handler);
});
// The handler also must go in a .js file
function handler() {
/* ... */
}
<script src="popup.js"></script>
// jQuery
$(document).ready(function() {
$("#click-this").click(handler);
});
function compile(qSel){
var matches = [];
var match = null;
var c = 0;
var html = $(qSel).html();
var pattern = /(<(.*?)on([a-zA-Z]+)\s*=\s*('|")(.*)('|")(.*?))(>)/mg;
while (match = pattern.exec(html)) {
var arr = [];
for (i in match) {
if (!isNaN(i)) {
arr.push(match[i]);
}
}
matches.push(arr);
}
var items_with_events = [];
var compiledHtml = html;
for ( var i in matches ){
var item_with_event = {
custom_id : "my_app_identifier_"+i,
code : matches[i][5],
on : matches[i][3],
};
items_with_events.push(item_with_event);
compiledHtml = compiledHtml.replace(/(<(.*?)on([a-zA-Z]+)\s*=\s*('|")(.*)('|")(.*?))(>)/m, "<$2 custom_id='"+item_with_event.custom_id+"' $7 $8");
}
$(qSel).html(compiledHtml);
for ( var i in items_with_events ){
$("[custom_id='"+items_with_events[i].custom_id+"']").bind(items_with_events[i].on, function(){
eval(items_with_events[i].code);
});
}
}
$(document).ready(function(){
compile('#content');
})
<body>
<a id=id_page href ="#loving" onclick="load_services()"> loving </a>
<script>
// This script MUST BE under the "ID" that is calling
// Do not transfer it to a differ DIV than the caller "ID"
document.getElementById("id_page").addEventListener("click", function(){
document.getElementById("mainbody").innerHTML = '<object data="Services.html" class="loving_css_edit"; ></object>'; });
</script>
</body>
<div id="mainbody" class="main_body">
"here is loaded the external html file when the loving link will
be clicked. "
</div>
<!doctype html>
<html>
<head>
<title>
Getting Started Extension's Popup
</title>
<script src="popup.js"></script>
</head>
<body>
<div id="text-holder">ha</div><br />
<a class="clickableBtn">
hyhy
</a>
</body>
</html>
<!doctype html>
window.onclick = function(event) {
var target = event.target ;
if(target.matches('.clickableBtn')) {
var clickedEle = document.activeElement.id ;
var ele = document.getElementById(clickedEle);
alert(ele.text);
}
}
window.onclick = function(event) {
var target = event.target ;
if(target.matches('.clickableBtn')) {
alert($(target).text());
}
}