所以基本上我试图把一些简单的事件监听器放在一起。我在我的HTML文件中添加了一个ID为“btn”的按钮,基本上我想添加一个鼠标悬停事件和一个点击事件。我知道怎么做,但是我想在每个事件发生时发出警报。有没有办法结束我的代码并把这一切都放在一个函数而不是两个?我只是不确定如何在同一个按钮上创建两个单独的警报语句。谢谢!
答案 0 :(得分:1)
您可以定义单个函数,然后将其作为 function menuPriviledge($dbCon){
$username = $_REQUEST['username'];
if($accessLevel = $this->dbConnection->query("SELECT access FROM UserProfiles WHERE username = '$username' LIMIT 0, 1")){
while($accessData = $accessLevel->fetch_assoc()){
$access = $accessData['access'];
}
}
的第二个参数引用。
示例:强>
if(isset($access)){
switch($access){
case 1:
$query = "SELECT * FROM CVCUserFilePrivileges";
if($files = $this->dbConnection->query($query)){
while($access = $files->fetch_assoc()){
printf("<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Directory Listings</a></li>
<li><a title=\"Ads Manager\"><i class=\"icon-laptop\"></i>Ads Manager<span>5</span></a>
<ul>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Paid Listings</a></li>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Listing Side Ads</a></li>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Scrap Metal Page Ads</a></li>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Coin Request Page Ads</a></li>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Form Page Ads</a></li>
</ul>
</li>", $access['directoryList'], $access['dlrPaidListing'], $access['listingSideAds'], $access['scrapMetalAds'], $access['coinRequestAds'], $access['dlrFormAds']);
}
}
break;
case 0:
$query = "SELECT * FROM CVCUserFilePrivileges";
if($files = $this->dbConnection->query($query)){
while($access = $files->fetch_assoc()){
printf("<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Directory Listings</a></li>
<li><a title=\"Ads Manager\"><i class=\"icon-laptop\"></i>Ads Manager<span>1</span></a>
<ul>
<li><a href=\"%s\"><i class=\"icon-angle-right\"></i>Dealer Paid Listings</a></li>
</ul>
</li>", $access['directory'], $access['dlrPaidListing']);
}
}
break;
default:
echo "There is an error, no access level set, contact your database administrator";
}
}
答案 1 :(得分:0)
下面的代码可以通过添加另一个参数来确定添加/删除,将两个函数合二为一,但你明白了。
(function() {
var btn = document.getElementById("btn");
addEvent(btn, "click mouseover", handler);
function handler ( event ) {
alert("This is the " + event.type + " handler.");
}
})();
function addEvent ( element, event, fnc ) {
var events = event.split(/\s/),
evt = "";
while ( evt = events.shift() ) {
((element.addEventListener) ? element.addEventListener(evt, fnc, false) : element.attachEvent("on" + evt, fnc));
}
}
function removeEvent ( element, event, fnc ) {
var events = event.split(/\s/),
evt = "";
while ( evt = events.shift() ) {
((element.removeEventListener) ? element.removeEventListener(evt, fnc, false) : element.detachEvent("on" + evt, fnc));
}
}
&#13;
<button id="btn" type="button">Click or Hover</button>
&#13;
答案 2 :(得分:-1)
我认为这就是你想要的:
var myfunc = function(event) {
var btn...
btn.addEventListener(event, function()...
}
你可以这样打电话:
Myfunc("click");