如何将样式应用于javascript创建的div?

时间:2016-01-29 11:34:03

标签: javascript html css

我使用javascript appendChild创建了两个div。我想将样式应用于那些div,但样式不起作用。这是JavaScript代码。 (我想仅在CSS文件中应用样式,而不是使用JavaScript。)

    #test1{
    text-align: justify;
    //text-justify:inter-word;
    letter-spacing: -0.5px;
    font-weight: bold;
    color: #333;
    font-weight: 500;
    position: relative;
    margin-bottom: 16px;
    padding-bottom: 8px;
    font-size: 19px;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    line-height: 1.25;
}
#test2{
    font-size: 130px;
}

}

我的test1和test2的CSS代码如下,这是无效的。

maximum

2 个答案:

答案 0 :(得分:1)

虽然我不知道您为什么要创建<test1><test2>标记,但您可以通过以下方式应用样式:

test1{
    text-align: justify;
    //text-justify:inter-word;
    letter-spacing: -0.5px;
    font-weight: bold;
    color: #333;
    font-weight: 500;
    position: relative;
    margin-bottom: 16px;
    padding-bottom: 8px;
    font-size: 19px;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    line-height: 1.25;
}
test2{
    font-size: 130px;
}

相反,您可以创建一个div并将id附加到这些div:

var elem1= document.createElement("div");
elem1.id = "test1";
elem1.appendChild(document.createTextNode(questions[i].ques));
document.getElementById("test").appendChild(elem1);

var elem2= document.createElement("div");
elem2.id = "test2";
elem2.appendChild(document.createTextNode(questions[i].ques));
document.getElementById("test").appendChild(elem2);

答案 1 :(得分:0)

使用using CsvHelper; using CSVSupplierClaims.Models; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Crm; using System.Data.SqlClient; using System.Data; namespace CSVSupplierClaims.Controllers { public class HomeController : Controller { public ActionResult Index(HttpPostedFileBase file) { string path = null; List<SupplierClaimsUploadDisplayList> supplierClaimsData = new List<SupplierClaimsUploadDisplayList>(); try { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName; file.SaveAs(path); var csv = new CsvReader(new StreamReader(path)); var supplierList = csv.GetRecords<SupplierClaimsUpload>(); foreach (var supplier in supplierList) { SupplierClaimsUploadDisplayList supplierUploadDisplay = new SupplierClaimsUploadDisplayList(); supplierUploadDisplay.Action = supplier.Action; supplierUploadDisplay.LineNo = supplier.LineNo; supplierUploadDisplay.TotalClaim = supplier.TotalClaim; supplierUploadDisplay.ClaimReference = supplier.ClaimReference; supplierUploadDisplay.Currency = supplier.Currency; supplierClaimsData.Add(supplierUploadDisplay); } } } catch { ViewData["error"] = "Uplaod failed"; } TempData["claimsResponse"] = supplierClaimsData; return View(supplierClaimsData); } public ActionResult ValidateClaims() { List<SupplierClaimsUploadDisplayList> supplierClaimsData = (List<SupplierClaimsUploadDisplayList>)TempData["claimsResponse"]; //= new List<SupplierClaimsUploadDisplayList>(); Supplier_Claim_Upload_Results supplierClaimUplaod = new Supplier_Claim_Upload_Results(); var sqlConnection = "data source=WMVSQL02;initial catalog=Embrace;integrated security=True;"; using (SqlConnection conn = new SqlConnection(sqlConnection)) { try { foreach (var claim in supplierClaimsData) { SqlCommand cmd = new SqlCommand(); cmd.CommandTimeout = 60; SqlDataReader reader; cmd.CommandText = "CRM.Supplier_Claim_Upload"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@Invoice", SqlDbType.NVarChar).Value = claim.LineNo; cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = claim.TotalClaim; cmd.Connection = conn; conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { claim.ST_Key = reader.GetString(reader.GetOrdinal("ST_Key")); if (claim.SupplierClaim != null) { claim.SupplierClaim = reader.GetString(reader.GetOrdinal("Supplier_Claim")); } else if (claim.SupplierClaim == null && claim.OrigInv == null && claim.SystemCost == null) { if (claim.Error != null) { claim.Error = reader.GetString(reader.GetOrdinal("Error")); } else if (claim.Error == null) { claim.SupplierClaim = reader.GetString(reader.GetOrdinal("Supplier_Claim")); } } if (claim.OrigInv != null) { claim.OrigInv = reader.GetString(reader.GetOrdinal("Orig_Inv")); } else if (claim.OrigInv == null) { if (claim.Error != null) { claim.Error = reader.GetString(reader.GetOrdinal("Error")); } else if (claim.Error == null) { claim.OrigInv = reader.GetString(reader.GetOrdinal("Orig_Inv")); } } if (claim.SystemCost != null) { claim.SystemCost = reader.GetString(reader.GetOrdinal("System_Cost")); } else if (claim.SystemCost == null) { if (claim.Error != null) { claim.Error = reader.GetString(reader.GetOrdinal("Error")); } else if (claim.Error == null) { claim.SystemCost = reader.GetString(reader.GetOrdinal("System_Cost")); } } } conn.Close(); } } catch (Exception ex) { ViewBag.Error = ex.Message + ex.InnerException; } } return View(supplierClaimsData); } } } 在文档中创建document.createElement("test1")元素。大概你希望这是<test1><div>?在这种情况下,您的代码应该是:

<span>

除了使用正确的元素之外,还需要设置id以便应用CSS。

然后再为&#34; test2,&#34;做同样的事情。创建适当的元素,然后将其上的id设置为&#39; test2&#34;。