2012-01-19 20 views
0

我有一个简单的,动态的电子邮件表单字段。用户提交电子邮件后,他们会收到消息“Got it!”。我需要字符串“明白了!”与我的css中的<p>标签具有相同的样式..但我不知道如何在javascript中分配属性,而代码的其他部分是在ruby中,我也不知道。这可能是一个简单的修复,但我找不到答案!如何将样式分配给Javascript变量?

这里是JavaScript:

var finalText = 
     'Got it!'; 


// Create a new div that will contain our thank you stuff 
var thankyouDiv = document.createElement("div"); 
thankyouDiv.id = "home_thankyou"; 
thankyouDiv.innerHTML = finalText; 

// Store reference to the formm 
var form = document.getElementsByTagName("form")[0]; 

// Add the new thankyoudiv before the form 
form.parentNode.insertBefore(thankyouDiv, form ); 

// remove the form tag alltogether 
form.parentNode.removeChild(form); 

这里是HTML/Ruby代码:

<%= form_for :home, :remote=>true, :update=>"emailsubmit", :url => {:controller=>"home", :action=>"create"} do |f| %> 
<div id=emailsubmit><%= email_field("infosignup", "email", :placeholder=>"Enter your email") %></div> 
<%= submit_tag("", :id=>"arrow2") %> 

任何帮助吗?

回答

3

它不是你需要的样式,而是你在其中编写的<div>

指定一个类的新的div:

// Create a new div that will contain our thank you stuff 
var thankyouDiv = document.createElement("div"); 
thankyouDiv.className = "someClass"; 
thankyouDiv.id = "home_thankyou"; 
thankyouDiv.innerHTML = finalText; 

然后在你的CSS,你已经定义<p>相同的规则定义类:

p, .someClass { 
    color: red; 
} 
+0

感谢迈克尔,它的工作完美! –