如何使用jQuery将HTML值(不是表)导出到CSV文件? [英] How to export html value(not table) to a csv file with jquery?

查看:56
本文介绍了如何使用jQuery将HTML值(不是表)导出到CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面用户可以动态添加或编辑html值,如下所示,我需要将其保存到数据库中,我找到了一种方法-php读取csv文件,然后更新到数据库,但是现在我的问题是

I have a page user can dynamic add or edit html value like below, and I need to save it to database, I found a way- php read csv file and then update to database, but now my problem is

如何使用jquery将html值(不是表格)导出到csv文件?

How to export html value(not table) to a csv file with jquery?

<div class="list">
    <div class="number">1</div>
    <div class="text">a</div>
    <div class="text">a</div>
</div>
<div class="list">
    <div class="number">1</div>
    <div class="text">a</div>
    <div class="text">a</div>
</div>
..

推荐答案

如果您没有包含逗号的字符串:

if you don't have comma-containing strings:

var csv = jQuery(".list").map(function(a,i){
  return $.trim($(this).text()).split(/\s*\n\s*/).join(",");
}).toArray().join("\r\n");

alert(csv);

您可以在 http://danml.com/js/download.js下载csv变量,例如:

you can use my downloader at http://danml.com/js/download.js to download the csv variable like:

 download(csv, "tabledata.csv", "text/csv");

这篇关于如何使用jQuery将HTML值(不是表)导出到CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆