如何从RJS模板更改html标签属性值?

时间:2022-11-26 23:10:55

Is it possible to change a html tag attribute value from an RSJ template? I know that there is a page.replace_html method, but it is not very useful in my case, since I have lengthy values of various attributes (such as alt, title of an image). What I want is change src attribute of a img tag in RJS. Is that possible at all?

是否可以从RSJ模板中更改html标记属性值?我知道有一页。replace_html方法,但是它在我的例子中不太有用,因为我有各种属性的长值(比如alt、图像标题)。我想要的是在RJS中修改img标签的src属性。这可能吗?

Thank you.

谢谢你!

3 个解决方案

#1


3  

EDIT: My first attempt didn't work, but this one does.

编辑:我的第一次尝试没有成功,但是这次成功了。

update_page do |page|
  page['image_id']['src'] = new_image_url
end

#2


3  

Slight modification to Can's answer. As suggested,

稍微修改一下Can的答案。作为建议,

update_page do |page|
    page['image_id']['src'] = new_image_url
end

translates to JS:

JS:翻译

$('image_id').src = new_image_url

This will work for some attributes that have direct JS DOM variable access, many don't. Luckily RJS is pretty good at rewriting JS method calls:

这将适用于一些具有直接JS DOM变量访问权限的属性,而许多属性没有。幸运的是,RJS非常擅长重写JS方法调用:

update_page do |page|
    page['image_id'].set_attribute('attrib', new_attrib_val)
end

translates to JS:

JS:翻译

$('image_id').setAttribute('attrib', new_attrib_val)

and you should be good to go.

你应该走了。


Small update: you may want to use write_attribute instead if you want IE compatibility.

小更新:如果你想要IE兼容性,你可以使用write_attribute。


Small update: in the above, [:src] and :attrib would probably be better style if these are static.

小更新:在上面,[:src]和:如果是静态的,attrib可能是更好的样式。

#3


0  

Depending on the Rails setup, the code above might work only if you exclude the page_update start and end lines -- I'm running Rails on mongrel on Windows 7, and putting the page[element][attribute] code on its own, outside of the update_page block, works fine, but including it inside the block breaks the code.

Rails设置,根据上面的代码可能只工作如果你排除page_update开始和结束行,我上运行Rails杂种在Windows 7中,并将页面(元素)[属性]代码自行update_page之外的块,没问题,但包括在打破了代码块。

#1


3  

EDIT: My first attempt didn't work, but this one does.

编辑:我的第一次尝试没有成功,但是这次成功了。

update_page do |page|
  page['image_id']['src'] = new_image_url
end

#2


3  

Slight modification to Can's answer. As suggested,

稍微修改一下Can的答案。作为建议,

update_page do |page|
    page['image_id']['src'] = new_image_url
end

translates to JS:

JS:翻译

$('image_id').src = new_image_url

This will work for some attributes that have direct JS DOM variable access, many don't. Luckily RJS is pretty good at rewriting JS method calls:

这将适用于一些具有直接JS DOM变量访问权限的属性,而许多属性没有。幸运的是,RJS非常擅长重写JS方法调用:

update_page do |page|
    page['image_id'].set_attribute('attrib', new_attrib_val)
end

translates to JS:

JS:翻译

$('image_id').setAttribute('attrib', new_attrib_val)

and you should be good to go.

你应该走了。


Small update: you may want to use write_attribute instead if you want IE compatibility.

小更新:如果你想要IE兼容性,你可以使用write_attribute。


Small update: in the above, [:src] and :attrib would probably be better style if these are static.

小更新:在上面,[:src]和:如果是静态的,attrib可能是更好的样式。

#3


0  

Depending on the Rails setup, the code above might work only if you exclude the page_update start and end lines -- I'm running Rails on mongrel on Windows 7, and putting the page[element][attribute] code on its own, outside of the update_page block, works fine, but including it inside the block breaks the code.

Rails设置,根据上面的代码可能只工作如果你排除page_update开始和结束行,我上运行Rails杂种在Windows 7中,并将页面(元素)[属性]代码自行update_page之外的块,没问题,但包括在打破了代码块。