0

我正在处理一个带有步骤的html表单,最后一页是一个摘要。这很好用,但现在我想显示基于 div 类而不是 div id 的值,这是因为我想多次显示该值。这是javascript:

 <SCRIPT TYPE="text/javascript">
 <!-- 
 function copyText(e)
 {    
  document.getElementById( e.name + 'preview' ).innerHTML = e.value;
 }
 //-->
 </SCRIPT> 


<script>
$("text1preview").draggable({ containment: '#dragcontainer' });
$("address1preview").draggable({ containment: '#dragcontainer' });
$("titlepreview").draggable({ containment: '#dragcontainer' });
$("zipcodepreview").draggable({ containment: '#dragcontainer' });
$("descriptionpreview").draggable({ containment: '#dragcontainer' });
$("wijkpreview").draggable({ containment: '#dragcontainer' });
$("address2preview").draggable({ containment: '#dragcontainer' });
$("tagspreview").draggable({ containment: '#dragcontainer' });
$(".catidpreview").draggable({ containment: '#dragcontainer' });
$("pricepreview").draggable({ containment: '#dragcontainer' });
</script>

这是摘要html:

  <tr>
    <td width="220">Samenvatting</td>
    </tr>
<tr>
    <td>Categorie:</td>
    <td><div class="categoryid"></div></td>
    </tr>
  <tr>
    <td>Titel van uw object:</td>
    <td><div id="titlepreview"></div></td>
   </tr>
 <tr>
    <td>Adres:</td>
    <td><div id="address1preview">  </div> </td>
    </tr>
    <tr>
    <td>Postcode</td>
    <td><div id="zipcodepreview"></div></td>
    </tr>
    <tr>
    <td>Plaats:</td>
    <td><div id="address2preview"></div></td>
    </tr>
  <tr>
    <td>Wijk:</td>
    <td><div id="wijkpreview"></div></td>
    </tr>
  <tr>
    <td>Omschrijving:</td>
    <td><div id="descriptionpreview"></div></td>
  </tr>
  <tr>
    <td>Tags:</td>
    <td><div id="tagspreview"></div></td>
  </tr>
  <tr>
    <td>Prijs per periode:</td>
    <td><div id="pricepreview"></div></td>
  </tr>
</table>
4

1 回答 1

1

你需要这样的东西

 function copyText(e)
 {
    var matchingElements = document.getElementsByClassName( e.name + 'preview' ),
        matchCount = matchingElements.length,
        value = (e.nodeName === 'SELECT') ? e.options[e.selectedIndex].text : e.value;

    for (var i =0; i< matchCount; i++){
       matchingElements[i].innerHTML = value;
    }
 }

有关 IE 兼容性,请参阅javascript document.getElementsByClassName 与 IE 的兼容性

于 2013-01-29T10:04:20.787 回答