ここで問題と思われるものについて具体的に別の質問を追加しましたが、もう少し詳細にするとうまくいくと思います。だから、これは私のコードです:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Prueba jQuery no event</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="loadUrls.php"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("a").click(function(ev) {
//ev.preventDefault();
// Defino las variables
var currentAnchor = jQuery(this);
var currentHref = currentAnchor.attr('href');
var curHref = currentHref.split('/');
var curHrefFinal = curHref[2].replace('www.', '');
if(jQuery.inArray(curHrefFinal,urlsFinal) > -1) {
// Evito que se visite el link directo
ev.preventDefault();
if (ev.metaKey || ev.ctrlKey) {
// Redirecciono el navegador a la página que queremos
window.open(currentHref + "?a=esta-funcionando-tambien");
} else {
// Redirecciono el navegador a la página que queremos
window.location = currentHref + "?a=esta-funcionando";
};
};
});
});
</script>
</head>
<body>
<p>Hola, <a href="http://www.avantrip.com">este es</a> un link.</p>
<p>Hola, <a href="http://espana.aula365.com/es/">este es</a> otro link.</p>
<p>Hola, <a href="http://stackoverflow.com">este es</a> otro link.</p>
<p>Hola, <a href="http://www.airborn.com.ar">este es</a> otro link.</p>
</body>
</html>
loadUrls.php ファイルが出力しています:
jQuery(document).ready(function() {
var urlsFinal = [
"avantrip.com.ar",
"avantrip.com",
"espana.aula365.com",
"almashopping.com",
"airborn.com.ar",
"1and1.mx",
"oiasdoiajsdoiasdoiasjdioajsiodjaosdjiaoi.com"
];
});
基本的に、クリックされているすべてのリンクの href 属性を、SQL データベースから動的に生成されている配列と比較しています。この配列は外部ファイルでホストする必要があるため、loadUrls.php ファイルです。
何らかの理由で、メイン コードで urlsFinal 変数が認識されません。配列をコピーしてメイン ファイルに貼り付けると、残りの部分は正しく機能します。問題は見つかりませんが、問題はないと思います。
何かご意見は?
ありがとう!