事件监听器 - 单击并鼠标悬停?

时间:2015-08-12 00:42:01

标签: javascript

所以基本上我试图把一些简单的事件监听器放在一起。我在我的HTML文件中添加了一个ID为“btn”的按钮,基本上我想添加一个鼠标悬停事件和一个点击事件。我知道怎么做,但是我想在每个事件发生时发出警报。有没有办法结束我的代码并把这一切都放在一个函数而不是两个?我只是不确定如何在同一个按钮上创建两个单独的警报语句。谢谢! enter image description here

3 个答案:

答案 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";
                }
            }

JSFIDDLE DEMO

答案 1 :(得分:0)

也许这会有所帮助?

下面的代码可以通过添加另一个参数来确定添加/删除,将两个函数合二为一,但你明白了。

&#13;
&#13;
(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;
&#13;
&#13;

答案 2 :(得分:-1)

我认为这就是你想要的:

var myfunc =  function(event) {
  var btn...
  btn.addEventListener(event, function()...
}

你可以这样打电话:

Myfunc("click");