RJS参考之JavaScriptGenerator

本文介绍了RJS模板中JavaScriptGenerator类的方法,包括写入原始JavaScript、返回DOM元素代理、赋值变量、显示警告对话框等功能。此外还介绍了如何通过RJS进行页面元素的隐藏、显示、替换等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RJS模板中的page即JavaScriptGenerator类的对象,它有如下方法:
[code]
<<(javascript)
[/code]
Writes raw JavaScript to the page.

[code]
[](id)
[/code]
Returns a JavaScriptElementProxy for the DOM element with the specified id. Methods can be called on the returned element. Multiple method calls can also be chained together.

[code]
assign(variable, value)
[/code]
Assigns a value to the JavaScript variable specified. Ruby objects are automatically converted to JavaScript objects by calling the object's to_json method if it has one, or inspect if it doesn't.

[code]
alert(message)
[/code]
Displays a JavaScript alert dialog box with the provided message.

[code]
call(function, arg, ...)
[/code]
Calls a JavaScript function and passes in zero or more arguments.

[code]
delay(seconds = 1)
[/code]
Executes the code within the block after delaying for the specified number of seconds.

[code]
draggable(id, options = {})
[/code]
Makes the DOM element specified by the id draggable.

[code]
drop_receiving( id, options = {})
[/code]
Makes the DOM element specified by the id receive dropped draggable elements. Draggable elements are created using the RJS draggable method or by using draggable_element() Scriptaculous helper.

[code]
hide(id, ...)
[/code]
Hides one or more DOM elements. Specify the elements to hide by their DOM ids.

[code]
insert_html(position, id, *options_for_render)
[/code]
Inserts the HTML into the specified position in relation to the element.
The available positions are:
:before
The content is inserted into the page before the element.
:after
The content is inserted into the page after the element.
:top
The content is inserted into the element before the element's existing content.
:bottom
The content is inserted into the element after the element's existing content.

[code]
redirect_to(location)
[/code]
Redirect the browser to the location specified. redirect_to() passes the location to url_for(), so any of the arguments you normally use with url_for() can also be used with redirect_to().

[code]
remove(id, ...)
[/code]
Removes one or more DOM elements from the page.

[code]
replace(id, *options_for_render)
[/code]
Replaces the entire element specified or outerHTML. replace is useful with partial templates so that the entire partial, which includes a container element, can be rendered and used to replace content during an RJS call. An example is an unordered list. A partial containing a <li> tag can be rendered instead of having to replace the innerHTML of the <li> . Replacing just the innerHTML would require the <li> tag to be moved out of the partial.

[code]
replace_html(id, *options_for_render)
[/code]
Replaces the content or innerHTML of the DOM element with the id with either a String or the output of a rendered partial template.

[code]
select(pattern)
[/code]
Selects DOM elements using CSS-based selectors. Returns a JavaScriptElementCollectionProxy that can then receive proxied enumerable methods. See the JavaScriptCollectionProxy methods in the next section.

[code]
sortable(id, options = {})
[/code]
Makes the element with the DOM ID id sortable using drag and drop. The options are the same as the options for ScriptaculousHelper#sortable_element().

[code]
show(id, ...)
[/code]
Makes one or more hidden DOM elements visible. As with hide(), the elements are specified by their DOM ids.

[code]
toggle(id, ...)
[/code]
Toggles the visibility of one or more DOM objects.

[code]
visual_effect(name, id = nil, options = {})
[/code]
Creates a new Scriptaculous visual effect to the element with the DOM id. See the following section on visual effects for a list of the visual effects shipping with Rails 1.1.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值