使用jquery创建文件或进行ajax调用

时间:2019-01-29 09:52:47

标签: jquery ajax

我有一个网站。本网站内是另一个网站的内容-例如,该内容

val divider = DividerItemDecoration(context,
        DividerItemDecoration.VERTICAL)

divider.setDrawable(ShapeDrawable().apply {
    intrinsicHeight = resources.getDimensionPixelOffset(R.dimen.dp_15)
    paint.color = Color.RED // note: currently (support version 28.0.0), we can not use tranparent color here, if we use transparent, we still see a small divider line. So if we want to display transparent space, we can set color = background color or we can create a custom ItemDecoration instead of DividerItemDecoration. 
})

recycler_devices.addItemDecoration(divider)

现在我要创建一个文件(example.html),其内容为div“ id = content”

我该怎么做?

如果有可能使用jquery进行操作,还是应该执行某种ajax调用,将内容发送到PHP文件并在PHP中完成工作?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

由于它是一种客户端语言,因此无法使用js创建文件,因此必须使用第二个选项,您可以向服务器端脚本发出ajax请求,该请求将接收内容并创建文件,例如:

JS:

 $.post('my_php_script.php', {content: $('#content').html()});

PHP:

<?php 
    $content = $_POST['content'];
    $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/example.html","wb");
    fwrite($fp, $content);
    fclose($fp);
?>