我動態地添加<script>
標籤與javascript & jQuery插入我的HTML文件,每當按下按鈕。所有內容都正確添加,但兩個.click
jQuery動作函數中只有一個正在工作。我似乎無法弄清楚爲什麼。控制檯沒有錯誤。我確定我在正確的地方使用了\'
和\"
。JavaScript做什麼預計
$('#classBox').append('<tr id=\"' + tempCHAR + tempNUM + '\">'
+ '<td>'
+ '<i id=\"' + tempCHAR + tempNUM + '\" class=\"icon-remove-sign\">'
+ '</i> '
+ '<a id=\"' + tempCHAR + tempNUM + '\"" href=\"https://cms.psu.edu/section/default.asp?id=' + urlHOLDER + '\" target="framehidden">'
+ tempYEAR + ': ' + tempCHAR + tempNUM + ', Section ' + tempSEC
+ '</a>'
+ '</td>'
+ '</tr>'
+ '<script>'
+ '$(\'#' + tempCHAR + tempNUM + '\').click(function() {'
+ '$(\'tr\').remove(\'tr#' + tempCHAR + tempNUM + '\');'
+ '});'
+ '$(\'a#\'' + tempCHAR + tempNUM + ').click(function() {'
+ '$(\'#framehidden\').attr(\'src\', $(\'a\', this).attr(\'href\')));'
+ '});'
+ '</script>');
縮進只是爲了我更容易閱讀我在做什麼。那不會是問題嗎?
如果這太不可能讀,那麼這裏是在正常的看法。
$('a#'IST130).click(function() {
$('#framehidden').attr('src', $('a', this).attr('href')));
});
$('#IST130').click(function() {
$('tr').remove('tr#IST130');
});
我有一個像其他30 .click
功能,所有的工作完美,但我只是無法弄清楚到底是怎麼回事錯在這裏。任何提示?
ID必須是唯一的。另外,這裏有一個拼寫錯誤:'$('a#'IST130)' –
考慮修改您的解決方案,只添加HTML內容並在追加後動態應用觸發器,而不是附加腳本標記。 – Quantastical
不需要像你那樣追加腳本。在你的主腳本塊中使用事件委託:'$(document).on('click',selector,function(){...' – Blazemonger