RJS:如何获取 DOM 元素的值和 innerHTML [英] RJS: How to get the value and innerHTML of a DOM element

查看:44
本文介绍了RJS:如何获取 DOM 元素的值和 innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 DOM 元素的值和 innerHTML 源分配给本地 ruby​​ 变量.但是,在我的方法中,我似乎无法使用 RJS 来完成此功能.不知道哪个方法可以实现,一句话就是:给一个js变量赋值给一个ruby局部变量,怎么做?

有限的情况是:page.assign 只能将 ruby​​ 变量值赋给 js 变量.page[] 只能获取一个DOM元素对象,不能返回其值或innerHTML

解决方案

您是说要通过 RJS 获取页面上某个元素的 innerHTML,将其分配给 Ruby 变量,并在 Ruby 代码中使用它RJS文件?例如:

<前># my_page.html<div id="foo">这是内容</div>

<前># my_page.js.rjs@some_var = page['foo'] # 期望得到这是内容"do_something_amazing(@some_var)

这在 RJS 中是不可能的.原因是您的 RJS 代码首先在服务器上执行,然后将其渲染结果发送到浏览器.

如果您需要在服务器端操作 DOM 元素的内容,您需要通过表单发布将这些内容发送到服务器.

I want to assign the value and the innerHTML source of a DOM element to the local ruby variables. However, in my method seemly i can not make use of the RJS to complete this function. I don't know which method can implement it, in one words is: to assign a js variable value to a ruby local variable, how to do it ?

The limited situations are : page.assign only can assign a ruby variable value to a js variable. page[] only can get one DOM element object, can not return it's value or innerHTML

解决方案

Are you saying you want to take the innerHTML of some element on the page via RJS, assign that to a Ruby variable, and use it in Ruby code in the RJS file? For example:

 # my_page.html
 <div id="foo">This is the content</div>

and

# my_page.js.rjs
@some_var = page['foo'] # expecting to get "This is the content"
do_something_amazing(@some_var)

This is not possible in RJS. The reason is that your RJS code executes first on the server, and then sends its rendered results to the browser.

If you need the contents of a DOM element to act upon on the server side, you'll need to send those contents to the server via a form post.

这篇关于RJS:如何获取 DOM 元素的值和 innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆