我有一个php文件,其中使用了一个脚本。
我正在尝试获取一个名为php (the_main_image)的图片,并使用jQuery在这个图片前面添加一组链接(以及一个当前运行良好的图标)。下面是我到目前为止所拥有的,但它目前破坏了函数
<script>
jQuery(document).ready(function($) {
$("#lesen").click(function() {
var bla = $('#my_hidden_input').val();
var test = "<?php the_main_image(); ?>";
$.ajax({
url: bla,
dataType: "text",
success: function(data) {
$(".text").html(data);
$(".text a[href^='http']").each(function() {
$(this).prepend(test);
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain=' + this.href + '">');
});
}
});
});
});
</script>如何在jQuery或其他类似的jQuery函数中使用包含php的预置变量值?
发布于 2018-01-05 09:01:21
嗨,我在我的项目中做过类似的事情,下面为我工作。
因为你不能直接用JavaScript编写php,所以你需要借助你的html。
在html中创建一个隐藏的输入域,如下所示
<input type="hidden" id="something" value="<?php the_main_image(); ?>"/>然后在jquery中阅读,如下所示
<script>
jQuery(document).ready(function($) {
$("#lesen").click(function() {
var bla = $('#my_hidden_input').val();
var test = $("#something").val();
$.ajax({
url: bla,
dataType: "text",
success: function(data) {
$(".text").html(data);
$(".text a[href^='http']").each(function() {
$(this).prepend(test);
$(this).prepend('<img src="https://www.google.com/s2/favicons?domain=' + this.href + '">');
});
}
});
});
});
</script>希望这能有所帮助。
发布于 2018-01-05 09:01:55
如果你在php中的函数没有回显结果。尝试添加回声。<?php echo the_main_image(); ?>。看起来你忘记回应结果了。
你可以从php中渲染结果,甚至可以在放在php文件中的js代码中使用它,但你应该记住,把php和js混合在一起是不好的做法,应该谨慎行事。
如何工作: php解释器在将静态html发送给用户之前读取您文件,并解释<?php ?>中所有与php相关的代码,如果您回显某些内容,它将出现在您执行此操作的行中(或者调用函数而不是回显)。有人提到,如果你用引号回显字符串,如果你在相同类型的引号中内联php标签,可能会中断js。
发布于 2018-01-05 10:36:42
尝试使用ajax在jquery函数中获取php值。
https://stackoverflow.com/questions/48105734
复制相似问题