Coffeescript jQuery 设置数据()

发布于 2024-12-24 20:06:34 字数 978 浏览 1 评论 0原文

我做错了什么?我试图将 .data() 分配给每个锚点。现在,该脚本仅删除标题,但不在 data() 中存储任何内容。当我尝试 console.log(title) 时,它写得很好,但仍然没有存储它。我正在使用 jQuery 1.7.1

我在咖啡中有以下几行

$(document).ready ->
    initBubble();

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr($(element))

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

编译后的输出如下

(function() {
  var initBubble, setInfoAttr;

  $(document).ready(function() {
    return initBubble();
  });

  initBubble = function() {
    var _this = this;
    return $('a[title]').each(function(index, element) {
      return setInfoAttr($(element));
    });
  };

  setInfoAttr = function(element) {
    var title;
    title = element.attr('title');
    return element.data('info', title).removeAttr('title');
  };

}).call(this);

What am I doing wrong? I am trying to assign .data() to each anchor. Now, the script only removes the title, but doesn't store anything in data(). When I tried console.log(title) it wrote the title fine, but still didn't store it. I'm using jQuery 1.7.1

I have the following lines in coffee

$(document).ready ->
    initBubble();

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr($(element))

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

The compiled output is as follows

(function() {
  var initBubble, setInfoAttr;

  $(document).ready(function() {
    return initBubble();
  });

  initBubble = function() {
    var _this = this;
    return $('a[title]').each(function(index, element) {
      return setInfoAttr($(element));
    });
  };

  setInfoAttr = function(element) {
    var title;
    title = element.attr('title');
    return element.data('info', title).removeAttr('title');
  };

}).call(this);

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

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

发布评论

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

评论(1

假面具 2024-12-31 20:06:34

也许稍微把事情打乱一下。我唯一能看到的是 initBubble 函数在调用时可能是未定义的。

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr $(element)

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

// A little shorcut for $(document).ready
$ ->
    initBubble()

Maybe shuffle things around a little bit. Only thing I can see is that the initBubble function may be undefined when it is called.

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr $(element)

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

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