模拟 JavaScript 中 元素的点击

发布于 2024-12-02 11:05:48 字数 488 浏览 1 评论 0原文

对于网站,我使用 jQuery supersized gallery 脚本: http://buildinternet。 com/project/supersized/slideshow/3.2/demo.html

正如您在演示中看到的,右下角有一个小箭头按钮,用于切换缩略图栏。配置文件中没有选项可以在打开站点时自动将其混合在一起。

所以我想我必须模拟点击该按钮(该按钮是托盘按钮,请参阅 HTML)。我尝试了这样的事情:

<script>
$(function() {
    $('#tray-button').click();
});
</script>

但是,这似乎在我测试过的任何浏览器中都不起作用。

有什么想法吗?

for a website, i am using the jQuery supzersized gallery script: http://buildinternet.com/project/supersized/slideshow/3.2/demo.html

As you can see in the demo, in the bottom right corner there is an little arrow button that toggles a thumbnail bar. There is no option in the config files to automatically blend this in when opening the site.

So i guess i have to simulate a click on that button (the button is the tray-button, see HTML). I tried something like this:

<script>
$(function() {
    $('#tray-button').click();
});
</script>

However, this doesnt seem to work in any browsers i tested.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

丢了幸福的猪 2024-12-09 11:05:48
$('#tray-arrow').click(function() {
 // prepare an action here, maybe say goodbye.
 //
 // if #tray-arrow is button or link <a href=...>
 // you can allow or disallow going to the link:
 // return true; // accept action
 // return false; // disallow 
});

$('#tray-arrow').trigger('click'); // this is a simulation of click
$('#tray-arrow').click(function() {
 // prepare an action here, maybe say goodbye.
 //
 // if #tray-arrow is button or link <a href=...>
 // you can allow or disallow going to the link:
 // return true; // accept action
 // return false; // disallow 
});

$('#tray-arrow').trigger('click'); // this is a simulation of click
小…红帽 2024-12-09 11:05:48

试试这个

$("#tray-arrow").live("click", function () {
               // do something

            });

Try this

$("#tray-arrow").live("click", function () {
               // do something

            });
眼睛会笑 2024-12-09 11:05:48

我假设您想在页面加载时弹出缩略图栏#thump-tray

方法如下:

找到文件 supersized.shutter.js 并找到以下代码:

// Thumbnail Tray Toggle
$(vars.tray_button).toggle(function(){
    $(vars.thumb_tray).stop().animate({bottom : 0, avoidTransforms : true}, 300 );
    if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-down.png");
    return false;
}, function() {
    $(vars.thumb_tray).stop().animate({bottom : -$(vars.thumb_tray).height(), avoidTransforms : true}, 300 );
    if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-up.png");
    return false;
});

在其后面添加:

$(vars.tray_button).click();

不要忘记在您的页面中(插件中的 demo.html),更改

<script type="text/javascript" src="theme/supersized.shutter.min.js"></script>

<script type="text/javascript" src="theme/supersized.shutter.js"></script>

I assume that you want to popup the thumbnail bar #thump-tray on page load.

Here's a way to do it:

locate the file supersized.shutter.js and find this code:

// Thumbnail Tray Toggle
$(vars.tray_button).toggle(function(){
    $(vars.thumb_tray).stop().animate({bottom : 0, avoidTransforms : true}, 300 );
    if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-down.png");
    return false;
}, function() {
    $(vars.thumb_tray).stop().animate({bottom : -$(vars.thumb_tray).height(), avoidTransforms : true}, 300 );
    if ($(vars.tray_arrow).attr('src')) $(vars.tray_arrow).attr("src", vars.image_path + "button-tray-up.png");
    return false;
});

After it, add:

$(vars.tray_button).click();

Dont forget in your page (demo.html in the plugin), to change

<script type="text/javascript" src="theme/supersized.shutter.min.js"></script>

to

<script type="text/javascript" src="theme/supersized.shutter.js"></script>
╄→承喏 2024-12-09 11:05:48

而不是使用

$(function(){
 //jquery magic magic
});

你可以尝试这个女巫将在加载整个页面(图像等)后发挥你的jquery魔法

$(window).load(function () {
  // jquery magic
});

并模拟你应该使用的点击 // 应该与 $('#tray-arrow').click( 相同);

$('#tray-arrow').trigger('click',function(){ })

例子:

$(window).load(function () {
  $('#tray-arrow').trigger('click',function(){ 
      alert('just been clicked!'); 
  })
});

instead of using

$(function(){
 //jquery magic magic
});

you culd try this witch will work your jquery magic after the full page is loaded (images etc)

$(window).load(function () {
  // jquery magic
});

and to simulate a click you culd use // shuld be the same as $('#tray-arrow').click();

$('#tray-arrow').trigger('click',function(){ })

example:

$(window).load(function () {
  $('#tray-arrow').trigger('click',function(){ 
      alert('just been clicked!'); 
  })
});
哥,最终变帅啦 2024-12-09 11:05:48

尝试

<script>
$(function() {
    $('#tray-arrow').click();
});
</script>

确保此代码是在您的轮播初始化之后。

try

<script>
$(function() {
    $('#tray-arrow').click();
});
</script>

Make sure that this code is after your carousel is initialized.

祁梦 2024-12-09 11:05:48

这看起来像是触发时机的问题。该插件还会在文档加载时加载,因此当您尝试绑定事件侦听器时,元素可能尚未创建。
也许您需要在 theme._init 函数之类的地方添加监听器
http://buildinternet.com/project/supersized/docs.html#theme-init
或类似的地方。

This looks like it's a problem of timing the trigger. The plugin also loads on document load, so maybe when you try to bind the event listener the element is not created yet.
Maybe you need to add the listener in something like the theme._init function
http://buildinternet.com/project/supersized/docs.html#theme-init
or somewhere similar.

弥枳 2024-12-09 11:05:48

问题可能是您的插件检测点击是由用户(真正的鼠标点击)发起的,还是通过代码发起的(通过使用 $('#id').click() 方法)。如果是这样,很自然的,通过代码点击锚元素是得不到任何结果的。

检查您的插件的源代码。

A problem might be that your plugin detects whether the click has been initiated by a user (real mouse click), or through code (by using $('#id').click() method). If so, it's natural that you can't get any result from clicking the anchor element through code.

Check the source code of your plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文