Php 函数的作用是:插入未解释的代码
我尝试使用jquery将php文件中的一段代码放入index.php文件中。 我使用jQuery的函数Php 函数的作用是:插入未解释的代码,php,jquery,function,load,alert,Php,Jquery,Function,Load,Alert,我尝试使用jquery将php文件中的一段代码放入index.php文件中。 我使用jQuery的函数load()来实现这一点,我对此没有问题 问题是这段代码没有正确解释。在本例中,我导入了一个span,为了查看这个span是否正确地导入到index.php中,我使用了jqueryclick函数 我要导入的php代码 //myText.php <?php echo '<span>theSpan</span>'; ?> index.php的php /
load()
来实现这一点,我对此没有问题
问题是这段代码没有正确解释。在本例中,我导入了一个span
,为了查看这个span是否正确地导入到index.php中,我使用了jqueryclick
函数
我要导入的php代码
//myText.php
<?php
echo '<span>theSpan</span>';
?>
index.php的php
//index.php
<body>
<div class="my-php">
</div>
</body>
因此,我用'test'
初始化变量strhp。
我试图用strhp=str
捕捉load函数的回调。
然后,我尝试使用html函数
将我的php div放入。
它不起作用。span在这里,文本是“span”
,但当我单击时,什么都没有
但是
$(document).ready(function(){
var StrPhp = 'test';
alert("1 - StrPhp : " +StrPhp); //Return 'test'
$(".my-php").load("myBets.php", function(str){
StrPhp = str;
alert("2 - StrPhp : " +StrPhp); //Return '<span>theSpan</span>'
});
alert("3 - StrPhp : " +StrPhp); //Return 'test'
alert("4 - StrPhp : " +StrPhp); //Return '<span>theSpan</span>' !!!
$(".my-php").html(StrPhp);
});
$(文档).ready(函数(){
var strhp=‘测试’;
警报(“1-strpp:+strpp);//返回'test'
$(“.myphp”).load(“myBets.php”,函数(str){
strpp=str;
警报(“2-strpp:+strpp);//返回“theSpan”
});
警报(“3-strpp:+strpp);//返回'test'
警报(“4-strhp:+strhp);//返回“theSpan”!!!
$(“.myphp”).html(strhp);
});
在html()函数
之前有两个警报,它就可以工作了!
span插入my php div,并进行解释。我的意思是,当我点击它时,我可以看到“成功:它是一个span”
。
如果没有这些警报,jQuery不会解释范围,当我单击时,什么也不会发生
显然,这并不是固定的,因为每次我遇到这个问题时,我都不想使用警报
所以如果有人能帮助我,我将不胜感激 使用参数:
$(document).ready(function(){
$(".my-php").load("myText.php",function () {
$("span").click(function(){
alert( "Success : it's a span !" );
})
});
});
你可以这样用
$(document).ready(function(){
$(".my-php").load("myText.php");
$(".my-php").on('click','span',function(){
alert( "Success : it's a span !" );
});
});
tl可能重复;dr:load()
函数是异步的,无法将事件绑定到代码运行时不存在的元素。当元素确实存在时,委派事件或运行代码。换句话说,当您告诉专业人员。。。他说的很明显,这个加载的最终用途不是点击功能,而是很多不应该在这个加载中的操作。那只是一场比赛test@user2488028然后在示例中提供更多的真实代码。我们没有水晶球来猜你想做什么do@charlietfl我的意思是说:如果我有很多单击功能或其他功能,我必须在这个加载中复制所有功能?@user2488028不,你可以使用事件委派。非常感谢你的回答@夏列特夫
$(document).ready(function(){
$(".my-php").load("myText.php",function () {
$("span").click(function(){
alert( "Success : it's a span !" );
})
});
});
$(document).ready(function(){
$(".my-php").load("myText.php");
$(".my-php").on('click','span',function(){
alert( "Success : it's a span !" );
});
});