2012-01-19 21 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

感謝邁克爾,它的工作完美! –