使用javascript / jquery的内部文本

时间:2018-02-05 13:14:40

标签: javascript jquery html datatables

我有一个数据表,我想提取第1列

Installing package into 'C:/Users/Stefan/Documents/R/win-library/3.4'
(as 'lib' is unspecified)
also installing the dependencies 'mime', 'stringi', 'evaluate', 'markdown', 'stringr', 'yaml'

trying URL 'http://rforge.net/src/contrib/mime_0.5.1.tar.gz'
Content type 'application/x-gzip' length 11695 bytes (11 KB)
==================================================
downloaded 11 KB

trying URL 'http://cran.rstudio.org/src/contrib/stringi_1.1.6.tar.gz'
Content type 'application/x-gzip' length 3647049 bytes (3.5 MB)
==================================================
downloaded 3.5 MB

trying URL 'http://rforge.net/src/contrib/evaluate_0.10.3.tar.gz'
Content type 'application/x-gzip' length 19612 bytes (19 KB)
==================================================
downloaded 19 KB

trying URL 'http://rforge.net/src/contrib/markdown_0.8.1.tar.gz'
Content type 'application/x-gzip' length 88252 bytes (86 KB)
==================================================
downloaded 86 KB

trying URL 'http://cran.rstudio.org/src/contrib/stringr_1.2.0.tar.gz'
Content type 'application/x-gzip' length 94095 bytes (91 KB)
==================================================
downloaded 91 KB

trying URL 'http://cran.rstudio.org/src/contrib/yaml_2.1.16.tar.gz'
Content type 'application/x-gzip' length 85275 bytes (83 KB)
==================================================
downloaded 83 KB

trying URL 'http://cran.rstudio.org/src/contrib/knitr_1.19.tar.gz'
Content type 'application/x-gzip' length 649805 bytes (634 KB)
==================================================
downloaded 634 KB

* installing *source* package 'mime' ...
** libs

*** arch - i386
Warning: running command 'make -f "C:/PROGRA~1/R/R-34~1.3/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-34~1.3/share/make/winshlib.mk" SHLIB="mime.dll" OBJECTS="init.o rawmatch.o"' had status 127
ERROR: compilation failed for package 'mime'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/mime'
In R CMD INSTALL
* installing *source* package 'stringi' ...
** package 'stringi' successfully unpacked and MD5 sums checked
Warning: running command 'sh ./configure.win' had status 127
ERROR: configuration failed for package 'stringi'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/stringi'
In R CMD INSTALL
* installing *source* package 'yaml' ...
** package 'yaml' successfully unpacked and MD5 sums checked
** libs

*** arch - i386
Warning: running command 'make -f "Makevars" -f "C:/PROGRA~1/R/R-34~1.3/etc/i386/Makeconf" -f "C:/PROGRA~1/R/R-34~1.3/share/make/winshlib.mk" SHLIB="yaml.dll" OBJECTS="api.o dumper.o emitter.o implicit.o loader.o parser.o r_emit.o r_ext.o r_parse.o reader.o scanner.o writer.o"' had status 127
ERROR: compilation failed for package 'yaml'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/yaml'
In R CMD INSTALL
ERROR: dependency 'mime' is not available for package 'markdown'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/markdown'
In R CMD INSTALL
ERROR: dependency 'stringi' is not available for package 'stringr'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/stringr'
In R CMD INSTALL
ERROR: dependency 'stringr' is not available for package 'evaluate'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/evaluate'
In R CMD INSTALL
ERROR: dependencies 'evaluate', 'markdown', 'stringr', 'yaml' are not available for package 'knitr'
* removing 'C:/Users/Stefan/Documents/R/win-library/3.4/knitr'
In R CMD INSTALL

The downloaded source packages are in
    'C:\Users\Stefan\AppData\Local\Temp\RtmpaiQed8\downloaded_packages'
There were 14 warnings (use warnings() to see them)
Error in library(knitr) : there is no package called 'knitr'
Execution halted

但它给了我一个这样的数组:

var usernames = dataTableTeamMembers.columns(1).data();

我只想要文字,而不是html。

如何提取标签的innertext?

2 个答案:

答案 0 :(得分:2)

有两种方法:

使用.column()(每个):



$(document).ready(function() {
    var dataTableTeamMembers = $('#example').DataTable();
    
    dataTableTeamMembers.column(1).data().each(function(username, index){
      console.log(username);
    });
} );

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"/>


<table id="example" class="display" width="100%">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Tiger</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Garrett</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Tiger</td>
        </tr>
        <tr>
            <td>4</td>
            <td>Ashton</td>
        </tr>
        <tr>
            <td>5</td>
            <td>Garrett</td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13;

.columns()(返回数组):

&#13;
&#13;
$(document).ready(function() {
    var dataTableTeamMembers = $('#example').DataTable();
    
    console.log(dataTableTeamMembers.columns(1).data()[0]);
} );
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.16/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"/>


<table id="example" class="display" width="100%">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>Tiger</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Garrett</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Tiger</td>
        </tr>
        <tr>
            <td>4</td>
            <td>Ashton</td>
        </tr>
        <tr>
            <td>5</td>
            <td>Garrett</td>
        </tr>
    </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据您的需要,您有3个选项

1使用DataTables API

var table = $('#your_table_id').DataTable().data();

for(var i = 0; i < table.length; i++)
{
    var tbl_obj = table[i];

    //do stuff

    console.log(JSON.stringify(table[i]))
}

2使用jquery获取所有行html

$('#your_table_id tr').each(function() {
    console.log($(this).html());    
 });

3按单元格类

获取行中的特定单元格
 $('#your_table_id tr').each(function() {
    var obj = $(this).find(".cell_class").html();    
 });

使用符合您需求的那个。