开发者

RJS Conditionals based on Element Style - Ruby on rails

开发者 https://www.devze.com 2023-02-22 10:01 出处:网络
I have some RJS in a rails app.I\'d like to be able to check the style of an html element before executing an action on that element.

I have some RJS in a rails app. I'd like to be able to check the style of an html element before executing an action on that element.

I have tried the following

page << "if ($('current_thread_partial').style.display != 'none'){"

                    puts "HAPPY"

                page << "}else{"

                    puts "SAD"

                page << "}"

But the conditional does not execute. In the example here both HAPPY and开发者_如何学Go SAD are printed to console regardless of the whether display is none or not. Any suggestions or another approach?

Cheers,

Slotishtype


To make this code working just do:

page <<JS
if ($('current_thread_partial').style.display != 'none'){
  alert('HAPPY');
}else{
  alert('SAD');
}
JS

This code writes all JavaScript into page variable so it'd be executed by browser.

Your puts between page << "" sentences executes on server side and simple had not been sent to user.

0

精彩评论

暂无评论...
验证码 换一张
取 消