簡體   English   中英

如何為Javascript變量分配樣式?

[英]How do I assign styling to a Javascript variable?

我有一個簡單的動態電子郵件表單字段。 用戶提交電子郵件后,他們會收到消息“知道了!”。 我需要字符串“知道了!” 在我的CSS中具有與<p>標記相同的樣式..但是我不知道如何在javascript中分配屬性,而代碼的另一部分在ruby中,我也不知道。 這可能是一個簡單的解決方法,但我找不到答案!

這是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") %>

有什么幫助嗎?

它不是您需要樣式化的JavaScript,而是您在其中編寫樣式的<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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM