我有这个简单的鬼文本实现:
HTML代码:
<div id="searchPanel">
<form method="get" id="searchBox" action="somePage.php">
<input class="ghText" type="text" name="query" value="search here"/>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery代码:
$(document).ready(function(){
$txtField = "#searchPanel form input.ghText";
var value = $($txtField).val();
$($txtField).focus(function(){
if($(this).val() == value)
$(this).val("").removeClass("ghText");
});
$($txtField).blur(function(){
if($(this).val()==""){
$(this).val(value).addClass("ghText");
}
});
});
Run Code Online (Sandbox Code Playgroud)
上面的例子不起作用.当用户将光标聚焦在搜索栏上时,由于某种原因,类"ghText"将不会被删除.
但是现在,如果我将"var value"(变量初始化)和"value"更改为"$ value",如下所示:
$value = $($txtField).val();
$(this).val($value).removeClass("ghText");
$(this).val($value).addClass("ghText");
Run Code Online (Sandbox Code Playgroud)
一切都很完美.
我可以去睡觉而不是太担心它..但我很好奇为什么会发生这样的事情?
是因为"this"没有引用正确的对象,或者是因为我尝试将jQuery对象存储在非jQuery变量中,或者是关于其他东西......有人能指出我出了什么问题吗?我一直以为"var x"与"$ x"相同..?
我正在尝试将事件处理程序附加到链接标记的加载事件,以在加载样式表后执行某些代码.
new_element = document.createElement('link');
new_element.type = 'text/css';
new_element.rel = 'stylesheet';
new_element.href = 'http://domain.tld/file.css';
new_element.addEventListener('load', function() { alert('foo'); }, false);
document.getElementsByTagName('head')[0].appendChild(new_element)
Run Code Online (Sandbox Code Playgroud)
我也试过onreadystatechange
new_element.onreadystatechange = function() { alert('foo'); }
Run Code Online (Sandbox Code Playgroud)
不幸的是,这两种方法都没有导致触发警报.此外,在使用addEventListener注册'load'事件的处理程序后,new_element.onload为null.这是正常的吗?
谢谢,安德鲁
ps:我可能不会使用任何框架来解决这个问题
我已经为JS文件提供了以下内容:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">!window.jQuery && document.write(unescape("%3Cscript src='/app_shared/script/jquery-1.6.1.min.js' type='text/javascript'%3E%3C/script%3E"))</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js"></script>
<script type="text/javascript">!window.jQuery.ui && document.write(unescape("%3Cscript src='/app_shared/script/jquery-ui.min.js' type='text/javascript'%3E%3C/script%3E"))</script>
Run Code Online (Sandbox Code Playgroud)
我怎样才能找到类似主题的东西?我可以从cdn下载它,如下所示:
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
Run Code Online (Sandbox Code Playgroud)
但是如何检测文件是否已下载,以便在失败时引用本地副本?我知道如何使用jQuery以编程方式添加本地副本,但我不知道如何检查CSS下载是否成功.此外,<link>标签下载是阻止还是异步?这也是一个问题.