我有一个 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 函数中使用包含 php 的 jQuery var 值?
最佳答案
嗨,我在我的项目中做了类似的事情,下面对我有用。
由于您无法直接用 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>
希望这有帮助。
关于php - 如何在 jQuery 函数中使用 php 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48105734/