jQuery:按键和叠加

时间:2013-01-31 08:37:34

标签: jquery overlay keypress

以下是代码:

1)jQuery:

<script src="Scripts/jquery.tools.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    $(function() {
        // if the function argument is given to overlay,
        // it is assumed to be the onBeforeLoad event listener
        $("a[rel]").overlay({
            mask: 'gray',
            effect: 'apple',
            onBeforeLoad: function() {
                // grab wrapper element inside content
                var wrap = this.getOverlay().find(".contentWrap");
                // load the page specified in the trigger
                wrap.load(this.getTrigger().attr("href"));
            }
        });
        $("#target").keypress(function(event) {
            // F1 pressed
            if (event.keyCode == 112) {
                alert("Help");
            }
        });
    });
</script>

2)加价:

<!-- external page is given in the href attribute (as it should be) -->
<a href="WebForm1.aspx" rel="#overlay" style="text-decoration:none">
  <!-- remember that you can use any element inside the trigger -->
  <button type="button">Show external page in overlay</button>
</a>

<!-- another link. uses the same overlay -->
<a href="WebForm2.aspx" rel="#overlay" style="text-decoration:none">
  <button type="button">Show another page</button>
</a>

<!-- overlayed element -->
<div class="apple_overlay" id="overlay">
  <!-- the external content is loaded inside this tag -->
  <div class="contentWrap"></div>
</div>

我在页面上有2个按钮,当点击任何一个漂亮的灰色图层时会显示一些文本。如果我像我们一样按F1,我想做同样的事情。在此示例消息框中出现,但需要用灰色图层替换。怎么做?

2 个答案:

答案 0 :(得分:0)

制作单独的功能:

function loadWrap (wrap) {
    var wrap = this.getOverlay().find(".contentWrap");

    // load the page specified in the trigger
    wrap.load(this.getTrigger().attr("href"));
}

然后在两个事件处理程序中调用此函数:

onBeforeLoad: function() {
    // grab wrapper element inside content
    loadWrap(this);
}

//////////////////

$("#target").keypress(function(event) {
    // F1 pressed
    if (event.keyCode == 112) {
        loadWrap($("a[rel]").first());
    }
});

答案 1 :(得分:0)

喜欢这个

$("body").keypress(function(event){
if(event.which == 12){  // keycode 12 for F1
  //trigger on click event 
   $("#element").click();
  //or write your code here
  code
 }
});