2010-09-28 84 views
1

我有我的網頁的JavaScript文件撰寫打破我

function roundthecon() { 
document.write(Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2)); 
} 

在此的Response.Write但它重新寫我的整個頁面,並與圓形數字替換它有沒有這樣做的任何其他方式沒有它重寫我的網頁?

感謝

傑米

UPDATE

的完整JavaScript

if (exchRate != "") { 

function roundthecon() { 
var value = Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2); 
$('.tablenotes > p > strong ').append(value); 
} 

function addCommas(nStr) { 
nStr += ''; 
x = nStr.split('.'); 
x1 = x[0]; 
x2 = x.length > 1 ? '.' + x[1] : ''; 
var rgx = /(\d+)(\d{3})/; 
while (rgx.test(x1)) { 
    x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
} 
return x1 + x2; 
} 


      // When the document is loaded.. 
      $(document).ready(function(){ 


        // Grab an array of the table cells 
        $('.evenprop table tr td:not(.title)').each(function(){ 

          // Calculate the pound price 
          var v_euro = $(this).html(); 

          if (v_euro != "N/A") { 

          var v_euro = v_euro.replace(/,/g,''); 
          var v_euro = v_euro.replace(/\u20AC/g, ''); 
          var v_euro = v_euro.replace(/£/g, ''); 

          var v_pound = Math.round(v_euro/exchRate); 
          v_pound = addCommas(v_pound); 

          // Create a new span element for the pound 


          // Insert it at the end of the table cell 

          if (exchRate == <%= Session("xch_dollar") %>) { 
          $(this).prepend("$"); 
          } 
          if (exchRate == <%= Session("xch_ntl") %>) { 
          $(this).prepend("X"); 
          } 
          if (exchRate == <%= Session("xch_euro") %>) { 
          $(this).append("&euro;"); 
          } 

          var o_span = $('<span/>').html(' <span style="font-weight:normal;" id="exchRate">(&pound;' + v_pound + ')</span>'); 
          $(this).append(o_span); 

          } 
        }); 

      }); 

      } 
+0

它總是避免使用document.write如果可能的話,像其他的答案提出很好的建議,但仍必須在這裏別的東西是錯誤的。 document.write()被明確定義爲停止頁面渲染並將其內容直接寫入調用它的腳本塊之後,然後繼續渲染。你必須做一些打破這種行爲的東西 - 你在哪裏打電話給roundthecon()? – AHM 2010-09-28 17:07:35

回答

4
function roundthecon() { 
    var value = Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2); 
    $('#some_element_id').text(value); 
} 

文件撰寫通常最好不要使用。它可以做一些奇怪的事情。

+0

感謝它的作品,但我需要追加它,它現在給我兩次價值?任何想法? – 2010-09-28 17:08:30

+0

'$('#some_element_id')。append(value);'兩次?你是否稱它爲兩次? – 2010-09-28 17:11:56

+0

請參閱我的更新以上我的源代碼 – 2010-09-28 17:26:38

0

嘗試這樣:

function roundthecon() { 
    document.innerHtml+=(Math.round(exchRate*Math.pow(10,2))/Math.pow(10,2)); 
}