属性更改的jQuery事件 [英] jQuery event on attribute change

查看:87
本文介绍了属性更改的jQuery事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var active = $(')我需要一个函数在divs data-page-index属性发生变化时运行。 swipeview-active'),
dpi = parseInt(active.attr('data-page-index')),
left = $('[data-page-index =''+ prev +' ]')。children(img)。eq(0),
right = $('[data-page-index =''+ next +''')。children(img)。eq (0);
$ b $(active.attr('data-page-index'))。change(function(){

right.clone(true)。css({'z fadeIn(2000).appendTo('#right');
left.clone(true).css({'z-index':5})。fadeIn(2000)。 appendTo('#left');
});

我尝试了change()函数,但似乎只适用于输入字段,


$ b $

  $(active.attr('data-page-index'))。change(function(){

$ b

是否有另一种实现此方法?谢谢。

解决方案

看来你仍然需要操纵DOM来触发事件吗?如果是这样,你可以操纵一个隐藏的输入值而不是操纵其他元素的数据属性,然后你可以使用 change trigger。

  var active = $('。swipeview-active'),
dpi = parseInt(active.attr('data-page-index')),
left = $('[data-page-index =''+ prev +']')。children(img ).eq(0),
right = $('[data-page-index =''+ next +''')。children(img)。eq(0),
dpiInput = $('。swipeview-active input:hide');

dpiInput.change(function(){
right.clone( true)。css({'z-index':5})。fadeIn(2000).appendTo('#right');
left.clone(true)。 css({'z-index':5})。淡入(2000).appendTo(#左);
});

触发器:

  $('。swipeview-active input:hide')。val(1).trigger('change'); 


I need a function to run when a divs data-page-index attribute changes

  var active = $('.swipeview-active'),
      dpi =  parseInt(active.attr('data-page-index')),
      left = $('[data-page-index="' +prev+ '"]').children("img").eq(0),
      right = $('[data-page-index="' +next+ '"]').children("img").eq(0);

    $(active.attr('data-page-index')).change(function(){

    right.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#right');
    left.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#left');
    });

I tried the change() function but that only seems to work with input fields,

    $(active.attr('data-page-index')).change(function(){    

is there another way of achieving this? Thanks.

解决方案

It seems that you still have to manipulate the DOM to trigger the event? If so, you can manipulate a hidden input's value instead of manipulating other elements' data attributes. Then you can use the change trigger.

  var active = $('.swipeview-active'),
      dpi =  parseInt(active.attr('data-page-index')),
      left = $('[data-page-index="' +prev+ '"]').children("img").eq(0),
      right = $('[data-page-index="' +next+ '"]').children("img").eq(0),
      dpiInput = $('.swipeview-active input:hide');

  dpiInput.change(function(){
    right.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#right');
    left.clone( true ). css({'z-index': 5}). fadeIn(2000).appendTo('#left');
  });

trigger:

$('.swipeview-active input:hide').val(1).trigger('change');

这篇关于属性更改的jQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆