【问题标题】:Perform a function per occurrence in a string在字符串中每次出现执行一个函数
【发布时间】:2013-07-08 10:49:29
【问题描述】:

在 jquery 中,如何在每次视频标签出现 在字符串中 时执行警报。我可以基于每个功能吗?假设该字符串名为“getTheCurrentResults”。

$(getTheCurrentResults["video"]).each(function ()
{
alert ("hello");
});

【问题讨论】:

标签: javascript jquery


【解决方案1】:

我不知道 getTheCurrentResults 是什么类型,但如果是 HTML,请使用烤的解决方案。

如果不是,并且您有一个字符串并且您想对每个项目执行操作:

$.each('27426472247'.match(/7/g),function (i,n){alert(n)})

例如,这将在每个“7”上触发一个函数。

【讨论】:

  • @AndrewJuniorHoward 如果您的字符串包含一些带有单词视频的文本(其中视频不代表标签)怎么办?
【解决方案2】:

如果我理解你的问题:

$('<div />').html($.trim(getTheCurrentResults)).find('video').each(function ()
{
   alert ("hello");
});

您将字符串包装在 jquery 对象中,然后在 jq 对象上使用横向方法。

【讨论】:

  • @RoyiNamir 不正确,如果
  • 我不明白。他说是 html 。视频元素在那里,那么在 body 元素下找到它有什么问题?
  • 即使是元素字符串 jQuery(theString) 也可以。
  • @RoyiNamir 好吧,你的意思是不使用 find 可能。我说的是这样的案例:jsfiddle.net/vPJFCvar getTheCurrentResults = '&lt;video&gt;&lt;/video&gt;';或者,请说明你的意思,我不明白
  • 从 childen 和 benieth 中查找搜索。你应该添加 addBack 这是新的 andSelf() jsbin.com/isexar/2/edit
猜你喜欢
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2016-07-16
  • 2022-10-15
  • 2019-06-23
相关资源
最近更新 更多