【问题标题】:How do I access the value of an HTML attribute from within CoffeeScript?如何从 CoffeeScript 中访问 HTML 属性的值?
【发布时间】:2016-06-19 23:31:46
【问题描述】:

我正在使用 summernote WYSIWYG editor 和 SimpleForm。

所以这个表单域:

<%= f.input :description, as: :summernote, placeholder: "Enter the description of the Job (e.g. 'Product Manager', 'Senior Ruby on Rails Developer')" %>

生成以下 HTML:

<textarea class="summernote optional" placeholder="Enter the description of the Job (e.g. 'Product Manager', 'Senior Ruby on Rails Developer')" data-provider="summernote" name="job[description]" id="job_description" style="display: none;"></textarea>

我的 SummerNote JS 执行如下:

ready = ->
  $('[data-provider="summernote"]').each ->
    $(this).summernote(height: 300, toolbar: [['style', ['bold', 'italic', 'underline', 'clear', 'fontname', 'fontsize', 'color']],
    ['font', ['strikethrough', 'superscript', 'subscript']], ['para', ['ul', 'ol', 'paragraph']],
    ['height', ['height']], ['insert', ['link', 'table', 'hr']]], placeholder: "Some placeholder copy")

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

但是,我想提取表单中指定的值(即&lt;textarea&gt; 字段中的占位符),而不是Some placeholder copy

我该怎么做?

【问题讨论】:

    标签: jquery ruby-on-rails coffeescript ruby-on-rails-5


    【解决方案1】:
    $(this).attr "placeholder" # => the attribute value
    

    这是基本的 jQuery。 this 是 textarea,.attr 是 jQuery 读取/写入属性值的方法。

    【讨论】:

    • 完美。正是我想要的。谢谢!
    • $(@),匹配 coffeescript 样式而不是 $(this)
    • @Exinferis 两者都有效。就个人而言,我更喜欢在仅指代对象本身时使用this,而在指代属性/方法时更喜欢使用@something。个人喜好,当然,但我只是觉得@ 本身看起来很奇怪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2019-10-17
    • 2015-01-23
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    相关资源
    最近更新 更多