首页 \ 问答 \ 如何让jQuery看到.clone()元素?(How to get jQuery to see .clone() elements?)

如何让jQuery看到.clone()元素?(How to get jQuery to see .clone() elements?)

对于我的问题的新生事道歉,我还是jQuery的新手。 我遇到了一个问题:有没有办法让jQuery能够查看和响应使用.clone()动态创建的元素?

目前,每当我克隆一些东西时,它显然不会被DOM“看到”,因此事件没有被注册。

这是我创建的一个混乱的jsFiddle: http//jsfiddle.net/3MvPL/

单击“提交”会显示alert() ,但元素的克隆版本不会。


Apologies for the newbishness of my question, I'm still new to jQuery. I've hit a problem: Is there a way to get jQuery to be able to see and react to elements dynamically created with .clone()?

At the moment, whenever I clone something it apparently isn't "seen" by the DOM, and so events are not registered.

Here's a messy jsFiddle I created: http://jsfiddle.net/3MvPL/

Clicking "submit" brings up the alert(), but a cloned version of the element doesn't.


原文:https://stackoverflow.com/questions/17527832
更新时间:2023-05-04 20:05

最满意答案

你可以这样做:

replacements = [ ["☺", ":)"], ["☹", ":("] ]
replacements.each {|replacement| str.gsub!(replacement[0], replacement[1])}

可能有一个更有效的解决方案,但这至少使代码更清洁


Since Ruby 1.9.2, String#gsub accepts hash as a second parameter for replacement with matched keys. You can use a regular expression to match the substring that needs to be replaced and pass hash for values to be replaced.

Like this:

'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*"
'(0) 123-123.123'.gsub(/[()-,. ]/, '')    #=> "0123123123"

In Ruby 1.8.7, you would achieve the same with a block:

dict = { 'e' => 3, 'o' => '*' }
'hello'.gsub /[eo]/ do |match|
   dict[match.to_s]
 end #=> "h3ll*"

相关问答

更多
  • 那么,你说的是你想要替换150,000行中的每一行中的600个字符串中的任何一个,并且你想每行运行一次替换操作? 是的,有办法做到这一点,但不是在PowerShell中,至少我想不出一个。 它可以在Perl中完成。 方法: 构建一个散列,其中的键是事物,值是事端。 使用|加入散列的键 符号,并将其用作正则表达式中的匹配组。 在替换中,插入一个表达式,该表达式使用捕获组的匹配变量从哈希中检索一个值 问题: 令人沮丧的是,PowerShell不会在正则表达式替换调用之外公开匹配变量。 它不适用于替换运算符,它不 ...
  • 你可以这样做: replacements = [ ["☺", ":)"], ["☹", ":("] ] replacements.each {|replacement| str.gsub!(replacement[0], replacement[1])} 可能有一个更有效的解决方案,但这至少使代码更清洁 Since Ruby 1.9.2, String#gsub accepts hash as a second parameter for replacement with matched keys. Yo ...
  • 这个转换是完全参数化的,并且不需要使用默认命名空间的任何技巧 : ...
  • 这是您可以使用的算法: 假设:字符串仅包含(A,B,C) 如果string仅由B's组成(无A / C),则输出=输入字符串。 除此以外: 按以下方式划分字符串。 如果子串由(B,A)或(B,C)组成,则将它们分开。 分别替换为A和C. 这将是答案。 例如。 让字符串为:“BBBAABABBBCCBCBBCACB”。 这将分为: “BBBAABABBB”“CCBCBBC”“A”“CB” 这将导致输出字符串为: ACAC 基本上,只是忽略所有的B's并用A's取代A's集群,用C取代C's集群。 Here i ...
  • 假设替换三元组从不重叠,那么您可以从最后开始并回到开头: String.prototype.replaceBetween = function(start, end, what) { return this.substring(0, start) + what + this.substring(end); }; var str = 'I want to go to India.' var triplets = [ [3,7,'want'] , [11,1 ...
  • 一个字符替换 如果您的输入字符串是H或A ,即一个字符,我建议您使用字典: mapper = {'H': ' ', 'A': '@'} home_away = mapper[home_away] 多字符替换 如果您有多个要替换的字符,则可以链接str.replace调用: home_away = home_away.replace('H', ' ').replace('A', '@') 或者,您可以使用str.translate和str.maketrans上面的mapper字典: home_away = ...
  • 如果你可以住在不同的地方持有者,你可以使用printf() : select printf('%s, %s!', 'Hello', 'world'); 或者,如果确保在您的字符串中没有出现任何或printf的%x占位符,则可以在应用printf之前用%s replace {%} 。 If you can live with a different place holder, you can use printf(): select printf('%s, %s!', 'Hello', 'world'); ...
  • 一个好方法是写一个空白字符串空白的关键字,然后吞噬空间直到下一个关键字。 方法签名将是: void blankKeyword(String content, String keyword, String next); 写起来应该不会太难。 A good way will be to write a that blanks out a keyword with empty string and gobbles up the space upto the next keyword. The method si ...
  • 您的ajax调用未设置为任何方法。 它会自动使用$_GET 。 如果你想让它使用$_POST ,你可以做速记: 使用POST获取的页面永远不会被缓存,因此jQuery.ajaxSetup()中的缓存和ifModified选项对这些请求没有影响。 $.post("indexpdf.php", data: str,function (result) { console.log("Success!"); }); 或者只需在您的ajax添加“method”选项即可。 $.ajax({ ur ...
  • 你可以使用String#scan和一个块: str = "aaaazzzazazaaaazzzazaaaazz" str.scan(/aaaa/) { |m| puts "#{$`}A#{$'}" } 输出: Azzzazazaaaazzzazaaaazz aaaazzzazazAzzzazaaaazz aaaazzzazazaaaazzzazAzz $`和$'是全局变量 ,引用最后一个匹配的左侧和右侧的字符串。 You could use String#scan with a block: str = ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。