php - 如何在 jQuery 函数中使用 php 值

标签 php jquery

我有一个 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/

相关文章:

php - Cakephp 模型类中的 save 方法会在创建和更新记录之间进行选择吗?

php - 使用 `BETWEEN 0 and 100000` 时 Wordpress 不会选择行

javascript - jQuery $ 变量差异

当前元素之后的 Jquery 目标跨度

javascript - 使用 jQuery 和固定步数的线性动画

javascript - 如何根据 SelectList 中选定的值更改 CodeMirror 编辑器的模式

php - 包含整数的 varchar 中的 OrderBy

php - 如何使用 Mysqli Php 返回受多次插入影响的行数?

php - 在 PDO 中强类型参数的原因?

javascript - 如何触发复选框上的更改或单击一行以选择表格行?