Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Javascript操作应用于由分部代码动态生成的元素?_Javascript_Jquery_Ruby On Rails 4 - Fatal编程技术网

如何将Javascript操作应用于由分部代码动态生成的元素?

如何将Javascript操作应用于由分部代码动态生成的元素?,javascript,jquery,ruby-on-rails-4,Javascript,Jquery,Ruby On Rails 4,我正在使用生成嵌套表单。这些嵌套表单的分部有一个文件输入,我正在使用它设置样式 生成文件输入的分部代码如下所示: $(document).on 'DOMNodeInserted', -> $(':file').filestyle buttonText: "Find File" buttonBefore: true ready = -> $(':file').filestyle buttonText: "Find File" b

我正在使用生成嵌套表单。这些嵌套表单的分部有一个文件输入,我正在使用它设置样式

生成文件输入的分部代码如下所示:

  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true
ready = ->
  $(':file').filestyle
    buttonText: "Find File"
    buttonBefore: true
  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true

$(document).ready(ready)
$(document).on('page:load', ready)
_image_fields.html.haml

如果在上一个视图的循环中加载了n次该部分,则该方法可以正常工作,但使用该部分动态添加的任何块都不会应用样式

这是我加载相关部分的coffescript的一部分:

ready = ->
  $(':file').filestyle
    buttonText: "Find File"
    buttonBefore: true

$(document).ready(ready)
$(document).on('page:load', ready)
如何绑定样式,使其应用于使用文件输入动态创建的任何元素

我知道如何绑定onclick事件,但是除了页面加载之外,仅仅是一个没有事件的类和/或元素类型呢

编辑进度!: 我通过绑定到DOMNodeInserted事件使其工作,如下所示:

  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true
ready = ->
  $(':file').filestyle
    buttonText: "Find File"
    buttonBefore: true
  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true

$(document).ready(ready)
$(document).on('page:load', ready)
它可以工作,但是我必须为动态创建的文件输入复制代码,或者换句话说,我的最终代码如下所示:

  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true
ready = ->
  $(':file').filestyle
    buttonText: "Find File"
    buttonBefore: true
  $(document).on 'DOMNodeInserted', ->
    $(':file').filestyle
      buttonText: "Find File"
      buttonBefore: true

$(document).ready(ready)
$(document).on('page:load', ready)

这是一个肮脏的解决方案,但它起到了作用。如果找到更好的解决方案,我将发布更新。

使用事件委派。对不起,我没有使用咖啡脚本,所以我不能用咖啡脚本给你一个确切的答案。如果你想要一个例子,我可以在JSFIDDLE中给你一个,如果我错了,请纠正我,但是他们只提到了点击事件。我试图通过Javascript操作使任何文件标记都能获得这种特殊的样式。Coffescript也没什么不同,如果你有javascript的解决方案,我可以用它。