2009-11-17 140 views
0

擴展我沒有構建的網站。我想能夠用參數調用ShowWindow proc。我會怎麼做?新的JQuery和Javascript。調用Javascript函數

default.aspx 

<script type="text/javascript" src="/scripts/jquery-1.2.6.js"></script> 
<script type="text/javascript"> 

$(document).ready(function() { 
    $('a#ShowWindow').click(function() { 
     window.open("TerminalInfo.aspx?", 'Info', 'scrollbars=yes,width=510,height=536'); 
    }) 
}); 

default.aspx.cs

動態構建ASPX ...

public static string ToHtml(this Location location) 
    { 
     var html = new StringBuilder(); 

     html.Append("<td><a href='#' id='ShowWindow'>ShowLetter</a></td>");    //This works 
     html.Append("<td><a href='#' id='ShowWindow(\"MyInfo.aspx\")'>Read More</a></td>"); //How can I do this? It doesn't work. 

     return html.ToString(); 
    } 
+1

在我看來就像你正在編寫的代碼,你不明白。你也玩過裝槍嗎? –

+1

@Josh Stodola:JavaScript比裝載槍更安全一些。每個人都必須在某個地方開始學習。 –

+0

我認爲喬希的觀點是,他看起來像在他開始編寫代碼並提出問題之後纔開始rtfm。 JavaScript中的事件已被過度記錄。 –

回答

6
public static string ToHtml(this Location location) 
{ 
    var html = new StringBuilder(); 

    html.Append("<td><a href='MyInfo.aspx' id='ShowWindow'>Read More</a></td>"); 

    return html.ToString(); 
} 

然後

$('a#ShowWindow').click(function(e) { 
    window.open($(this).attr("href"), 'Info', 'scrollbars=yes,width=510,height=536'); 
    e.preventDefault(); 
}) 

這是一種有點不同的方法,但如果JavaScript不可用,它會降低得更好。

更新(在表上的幾個環節工作)

$('table a').click(function(e) { 
    window.open($(e.target).attr("href"), 'Info', 'scrollbars=yes,width=510,height=536'); 
    e.preventDefault(); 
}); 
+1

+1是唯一沒有使用onclick屬性的解決方案。我開始失去信心。 –

+0

是的,比我的好。我只是在迴應這個明顯的錯誤。我想如果他要在這個項目上學習JavaScript,我們應該幫助他正確學習。 – sheats

+0

我看到的唯一問題是你沒有返回false,所以頁面會改變 – Shawn

-2
var strattonn = {}; 
strattonn.openWindow = function(url, title) { 
    window.open(url, title, 'scrollbars=yes,width=510,height=536'); 
    return false; 
} 


public static string ToHtml(this Location location) 
{ 
    var html = new StringBuilder(); 
    html.Append("<td><a href='#' onclick='return strattonn.openWindow('MyInfo.aspx', 'My Info')'>Read More</a></td>"); 
    return html.ToString(); 
} 
+0

好吧,所以我知道onclick屬性是一個不好的做法; p – Shawn