2013-10-01 69 views
1

我是一個使用XHTML模板創建電子商務網站的新手。我想在某些項目下添加免責聲明,但不是全部。爲避免在每個條目下鍵入免責聲明(並且如果免責聲明發生變化,將來避免出現問題),我希望使用javascript創建一個副本塊,當我指向它時,會添加免責聲明。我已經成功完成了(yah!),但是在免責聲明中是指向pdf的鏈接。當我使用html鏈接到PDF時,它失敗。我知道這可能是因爲我沒有正確的語法,因爲HTML是JavaScript代碼中的「內部」。有人可以幫忙嗎?如何在JavaScript中創建指向PDF的鏈接?

這是我有:

//<![CDATA[ 
function openPDF(file) 
{ 
window.open (file, 'resizable,scrollbars'); 
} 
//]]> 
</script> 

<script type="text/javascript"> 
//<![CDATA[ 
onload=function() 
{ 
var txt=document.getElementById("myDiv") 
txt.innerHTML="Photos do not represent actual size. Refer to measurements for sizing. 
Measurements are approximate. Colors shown may differ depending on your computer 
settings. Colors described are subjective and colors may vary from piece to piece, 
depending on the natural properties of the stones. To learn more, see our <a href="#" 
onClick="openPDF('www.shop.site.com/media/JewelryGuide.pdf')">Jewelry Guide</a>."; 
} 
//]]> 
</script> 

下面是我用它調用的代碼:

<div id="myDiv"></div>` 

回答

0

1)您在該文本字符串中有換行符。 2)你需要轉義幾個引號。我選擇交換引號並繞過文件名。

txt.innerHTML = 'Photos do not represent actual size. Refer to measurements for sizing. Measurements are approximate. Colors shown may differ depending on your computer settings. Colors described are subjective and colors may vary from piece to piece, depending on the natural properties of the stones. To learn more, see our <a href="#" onClick="openPDF(\'www.shop.site.com/media/JewelryGuide.pdf\')">Jewelry Guide</a>.'; 

如果你不想一個長行,你可以向上突破,像這樣的臺詞:

txt.innerHTML = 'Photos do not represent actual size.' + 
'Refer to measurements for sizing. Measurements are approximate.' + 
'Colors shown may differ depending on your computer settings.' + 

等等

甚至:

txt.innerHTML = [ 
    'Photos do not represent actual size.', 
    'Refer to measurements for sizing. Measurements are approximate.', 
    'Colors shown may differ depending on your computer settings.' 
].join(''); 
1

嗨像下面的函數會做,我相信工作..

function openPdf(e, path) { 
    // stop the browser from going to the href 
    e = e || window.event; // for IE 
    e.preventDefault(); 

    // launch a new window with your PDF 
    window.open(path, 'somename', ... /* options */); 

} 

嗨,我已經做了一個小提琴希望它有幫助... http://jsbin.com/aQUFota/1/edit

相關問題