我正在使用jQuery中的.load
函数将页面的一部分加载到另一个页面
$("#loadingDockShotPg").load("include/shot_comments.php?id="+get['id']);
在我的文件中被加载到id为“loadingDockShotPg”的div中,其中有一些元素,我希望受到jQuery命令的影响。
使用.load
<textarea id='newNoteTextarea' class='width100 hidden' placement='Write a comment...'></textarea>
放置在上面的HTML放在
中的父文件中的jQuery $(document).ready(function(){
$("input[placement], textarea[placement]").each(function(){
var orAttr = $(this).attr("placement");
$(this).val(orAttr);
$(this).focus(function(){
if($(this).val() == orAttr)
{
$(this).val("");
}
});
$(this).blur(function(){
if($(this).val() == "")
{
$(this).val(orAttr);
}
});
});
});
问题是我的textarea不受jQuery的影响,该jQuery应该将placement属性放在textarea的值中。为了完成这项工作,我该怎么做?
答案 0 :(得分:1)
使用.live()
时,您可能希望使用.on()
(技术上已弃用)或.load()
附加/绑定到所选元素上的事件
答案 1 :(得分:1)
您的.load()
是“直播功能”。您在加载DOM后将内容添加到DOM中。在使用.each()
添加内容之前,您的.load()
功能已经执行。要解决此问题,您可以在.each()
作为回调完成时运行.load()
功能。
我应该这样做:
$(function(){
var inputElements = $("input[placement], textarea[placement]");
function setInputValue() {
var self = $(this),
orAttr = self.attr("placement");
self
.val(orAttr)
.focus(function(){
if(self.val() == orAttr)
{
self.val("");
}
})
.blur(function(){
if(self.val() == "")
{
self.val(orAttr);
}
});
}
inputElements.each(setInputValue);
$("#loadingDockShotPg").load("include/shot_comments.php?id="+get['id'], function(){
inputElements = $("input[placement], textarea[placement]");
inputElements.each(setInputValue);
});
});