2012-02-02 13 views
12

का उपयोग कर गतिशील रूप से बनाई गई पंक्ति में सीएसएस क्लास जोड़ना मेरे पास एक jsp पृष्ठ है जिसमें तालिका के पंक्तियां गतिशील रूप से जोड़े जाती हैं। यहां मैं अपने पिछले प्रश्न में से एक की तुलना में एक अलग जावा स्क्रिप्ट का उपयोग कर रहा हूं। यहां मैं तालिका कॉलम में तत्व जोड़ सकता हूं, लेकिन मैं स्टाइल क्लास को लागू नहीं कर सका जो पहले ही एक सीएसएस फ़ाइल में परिभाषित है।जावा स्क्रिप्ट

मेरी जावा स्क्रिप्ट समारोह है

function addrow(tableID) { 
    try{ 
    var table = document.getElementById(tableID); 
    var rowCount = table.rows.length; 
    var row = table.insertRow(rowCount-1); 


    var i=0; 
    var newcell = row.insertCell(i); 
    newcell.innerHTML ='<h4>Type &nbsp;&nbsp;&nbsp;</h4>'; 

    i++; 
    newcell = row.insertCell(i); 
    newcell.innerHTML ='<input type="text" name="type7" id="type8" size="30"/>'; 
    i++; 
    newcell = row.insertCell(i); 
    newcell.innerHTML =''; 
    i++; 
    newcell = row.insertCell(i); 
    newcell.innerHTML ='<h4>Description &nbsp;&nbsp;&nbsp;</h4>'; 
    i++; 
    newcell = row.insertCell(i); 
    newcell.innerHTML ='<textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea>'; 

}catch(e) { 
    alert(e); 
} 
} 

मेरे एचटीएमएल हिस्सा

<table id="table1" width="792" border="0"> 

<tr class="rowdiv"> 
     <td class="formlabel"><h4>Type &nbsp;&nbsp;&nbsp;</h4></td> 
     <td class="formfield"><input type="text" name="type7" id="type8" size="30"/></td> 
     <td class="formgap"></td> 
     <td class="formlabel"><h4>Description &nbsp;&nbsp;&nbsp;</h4></td> 
     <td class="formfield"><textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea></td> 
     </tr> 

     <tr class="rowdiv"> 
     <td width="170" class="formlabel">&nbsp;</td> 
     <td class="formfield">&nbsp;</td> 
     <td class="formgap"></td> 
     <td class="formlabel">&nbsp;</td> 
     <td class="formfield"><h6 onclick="addrow('table1')">Add policy</h6></td> 
     </tr> 

    </table> 

मैं भी नव निर्मित पंक्तियों में एक ही शैली कक्षाओं formlabel, formfield और formgap लागू करने की आवश्यकता है।

मैंने गुगले में कोशिश की लेकिन कुछ कोड प्राप्त किए जो स्टाइल गुणों को एक-एक करके निकाल देंगे और नई पंक्ति में कॉपी करेंगे। लेकिन यह वही नहीं है जो मुझे चाहिए, मुझे कक्षा के नाम खुद को रखना होगा।

मेरी सीएसएस हिस्सा

.formlabel{       /* fields label's style */ 
    text-align: right; 
    font:Verdana, Geneva, sans-serif; font-weight: lighter; 
    margin: 0px; 
    padding: 0px; 
    text-transform: capitalize; 
    color:#000000; 
} 
.formlabel a{       /* fields label's style */ 
    text-align: right; 
    font:Verdana, Geneva, sans-serif; font-weight: bold; 
    margin: 0px; 
    padding: 0px; 
     text-decoration:none; 
    text-transform: capitalize; 
    color:#FF0000; 
} 
.formlabel a:HOVER{       /* fields label's style */ 
    text-align: right; 
    font:Verdana, Geneva, sans-serif; font-weight: bold; 
    margin: 0px; 
    padding: 0px; 
     text-decoration:none; 
    text-transform: capitalize; 
    color:navy; 
} 
.formfield {       /* field style */ 
    text-align: left; 
    margin-left:1px; 
    font:Verdana, Geneva, sans-serif; 
    text-transform: capitalize; 

    color:#000000; 
} 
.formfield textarea{       /* field style */ 
    text-align: left; 
    margin-left:1px; 
    font:Verdana, Geneva, sans-serif; 
    text-transform: none; 
width:185px; 
    color:#000000; 
} 
.formfield a{       /* field style */ 
    text-align: left; 
    margin-left:1px; 
    font:Verdana, Geneva, sans-serif; font-weight: bold; 
     text-decoration:none; 
    text-transform: capitalize; 
    color:#FF0000; 
} 
.formfield a:HOVER{       /* field style */ 
    text-align: left; 
    margin-left:1px; 
    font:Verdana, Geneva, sans-serif; font-weight: bold; 
     text-decoration:none; 
    text-transform: capitalize; 
    color:navy; 
} 
.loginformfield {       /* field style */ 
    text-align: left; 
    margin-left:1px; 
    font:Verdana, Geneva, sans-serif; 
} 
.formfield input {text-transform: capitalize;`font:Verdana, Geneva, sans-serif;} 

.formlabel h5{margin: opx;padding: opx; font-weight: lighter;} 
.formfield h6{margin: opx;padding: opx; font-weight: lighter;} 
+1

बस एक नोट के रूप में, यह आप डायनामिक रूप से तैयार तत्वों में से एक पर एक 'id' विशेषता स्थापित कर रहे हैं, लेकिन आप केवल एक स्थिर मूल्य का उपयोग कर रहे तरह दिखता है। तत्वों पर आईडी अद्वितीय होने की आवश्यकता है। –

+3

क्या आपने newcell.class = newcell.className = "yourclass" को आजमाया था? – Candide

उत्तर

33

एक तत्व के वर्ग (ते) className संपत्ति में जमा हो जाती है, तो कोशिकाओं आप एक वर्ग में जोड़ना चाहते हैं के लिए newcell.className = 'yourclassname'; बुला की कोशिश है।

+1

@sarin मुझे खुशी है कि यह काम करता है। हालांकि, अगर आपके द्वारा प्रदान किए गए दो उत्तरों में से एक ने आपकी समस्या को हल करने में मदद की है, तो मैं उलझन में हूं कि आपने उनमें से किसी एक को उत्तर के रूप में क्यों स्वीकार नहीं किया है, और इसके बजाय अपने अब काम करने वाले कोड को उत्तर के रूप में पोस्ट करने का विकल्प चुना है और उसे स्वीकार कर लिया है। .. –

+0

भी इस सवाल पर गौर करें .. http://stackoverflow.com/q/9110044/1184697 –

+1

मैं सबसे पुराने पोस्ट करने के लिए मतदान किया ही .. –

2
var table = document.getElementById(tableID); 
var rowCount = table.rows.length; 
var row = table.insertRow(rowCount-1); 
row.className = "rowdiv"; 

var i=0; 
var newcell = row.insertCell(i); 
newcell.innerHTML ='<h4>Type &nbsp;&nbsp;&nbsp;</h4>'; 
newcell.className = "formlabel"; 

i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<input type="text" name="type7" id="type8" size="30"/>'; 
newcell.className = "formfield"; 

i++; 
newcell = row.insertCell(i); 
newcell.innerHTML =''; 
newcell.className = "formgap"; 

i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<h4>Description &nbsp;&nbsp;&nbsp;</h4>'; 
newcell.className = "formlabel"; 

i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea>'; 
newcell.className = "formfield"; 
+1

धन्यवाद अपनी कार्यशील –

+1

कृपया इस सवाल पर गौर .. http://stackoverflow.com/q/9110044/1184697 –

1
var newcell = row.insertCell(i); 
newcell.innerHTML ='<h4>Type &nbsp;&nbsp;&nbsp;</h4>'; 
newcell.className = 'formlabel'; 
i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<input type="text" name="type7" id="type8" size="30"/>'; 
newcell.className = 'formfield'; 
i++; 
newcell = row.insertCell(i); 
newcell.innerHTML =''; 
newcell.className = 'formgap'; 
i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<h4>Description &nbsp;&nbsp;&nbsp;</h4>'; 
newcell.className = 'formlabel'; 
i++; 
newcell = row.insertCell(i); 
newcell.innerHTML ='<textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea>'; 
newcell.className = 'formfield'; 
+1

कृपया इस सवाल पर गौर .. http://stackoverflow.com/q/9110044/1184697 –

संबंधित मुद्दे