jQuery:删除前两行的最后一行

时间:2017-03-13 15:59:28

标签: jquery

如何使用jquery从前两行中删除最后一个td?我有这个: $('#mainmenu tr td:last-child').remove();

这会删除所有行,以便如何删除第一个 2行

jsfiddle:http://jsfiddle.net/XbGSV/62/



$('#mainmenu  tr td:last-child').remove();

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <table cellpadding="0" cellspacing="5" id="mainmenu">
        <tbody>
            <tr>
                <td valign="top" class="menu_sub">
                    something 1
                </td>
                <td valign="top" class="menu_sub">
                    something 2
                </td>
                <td valign="top" class="menu_sub">
                    something 3
                </td>
            </tr>
             <tr>
                <td valign="top" class="menu_sub">
                    something 11
                </td>
                <td valign="top" class="menu_sub">
                    something 22
                </td>
                <td valign="top" class="menu_sub">
                    something 33
                </td>
            </tr>
             <tr>
                <td valign="top" class="menu_sub">
                    something 11
                </td>
                <td valign="top" class="menu_sub">
                    something 22
                </td>
                <td valign="top" class="menu_sub">
                    something 33
                </td>
            </tr>
        </tbody>
    </table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

您可以使用:lt选择器选择前n个元素

//Check to see if this is acutally a form post and then set session
if (isset($_REQUEST['pPageItemID'])){
    $_SESSION['ITEM_IDS'] = $itemAdded;
}

答案 1 :(得分:1)

使用jQuery :lt()伪类选择器来获取小于提供的索引的元素。

// Important modules this config uses
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OfflinePlugin = require('offline-plugin');

module.exports = require('./webpack.base.babel')({
  // In production, we skip all hot-reloading stuff
  entry: [
    'script!jquery/dist/jquery.min.js',
    // 'script!materialize-css/dist/js/materialize.min.js',
    'script!style/js/materialize.min.js',
    'script!style/js/init.js',
    path.join(process.cwd(), 'app/app.js'),
  ],

  // Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: '[name].[chunkhash].chunk.js',
  },

  // We use ExtractTextPlugin so we get a separate CSS file instead
  // of the CSS being in the JS and injected as a style tag
  cssLoaders: ExtractTextPlugin.extract({
    fallbackLoader: 'style-loader',
    loader: 'css-loader?modules&-autoprefixer&importLoaders=1!postcss-loader',
  }),

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      children: true,
      minChunks: 2,
      async: true,
    }),

    // OccurrenceOrderPlugin is needed for long-term caching to work properly.
    // See http://mxs.is/googmv
    new webpack.optimize.OccurrenceOrderPlugin(true),

    // Merge all duplicate modules
    new webpack.optimize.DedupePlugin(),

    // Minify and optimize the JavaScript
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false, // ...but do not show warnings in the console (there is a lot of them)
      },
    }),

    // Minify and optimize the index.html
    new HtmlWebpackPlugin({
      template: 'app/index.html',
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true,
      },
      inject: true,
    }),

    // Extract the CSS into a separate file
    new ExtractTextPlugin('[name].[contenthash].css'),

    // Put it in the end to capture all the HtmlWebpackPlugin's
    // assets manipulations and do leak its manipulations to HtmlWebpackPlugin
    new OfflinePlugin({
      relativePaths: false,
      publicPath: '/',

      // No need to cache .htaccess. See http://mxs.is/googmp,
      // this is applied before any match in `caches` section
      excludes: ['.htaccess'],

      caches: {
        main: [':rest:'],

        // All chunks marked as `additional`, loaded after main section
        // and do not prevent SW to install. Change to `optional` if
        // do not want them to be preloaded at all (cached only when first loaded)
        additional: ['*.chunk.js'],
      },

      // Removes warning for about `additional` section usage
      safeToUseOptionalCaches: true,

      AppCache: false,
    }),
  ],
});

&#13;
&#13;
$('#mainmenu  tr:lt(2) td:last-child').remove();
&#13;
$('#mainmenu  tr:lt(2) td:last-child').remove();
&#13;
&#13;
&#13;

或者使用:nth-child()伪类选择器和自定义<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <table cellpadding="0" cellspacing="5" id="mainmenu"> <tbody> <tr> <td valign="top" class="menu_sub"> something 1 </td> <td valign="top" class="menu_sub"> something 2 </td> <td valign="top" class="menu_sub"> something 3 </td> </tr> <tr> <td valign="top" class="menu_sub"> something 11 </td> <td valign="top" class="menu_sub"> something 22 </td> <td valign="top" class="menu_sub"> something 33 </td> </tr> <tr> <td valign="top" class="menu_sub"> something 11 </td> <td valign="top" class="menu_sub"> something 22 </td> <td valign="top" class="menu_sub"> something 33 </td> </tr> </tbody> </table>等式,其中-n + 2的值为n,因此结果为0, 1, 2,...所以前2个被选中的剩余将被忽略(2, 1, 0, -1...不是有效的元素索引,因为它是1 - 索引)。

0, -1,...

&#13;
&#13;
$('#mainmenu  tr:nth-child(-n+2) td:last-child').remove();
// or
$('#mainmenu  tr:not(:nth-child(n+3)) td:last-child').remove();
&#13;
$('#mainmenu  tr:nth-child(-n+2) td:last-child').remove();
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用:lt选择器选择前n个元素

#example  tr:lt(2) td:last-child

$('#example  tr:lt(2) td:last-child').remove();

jsfiddle:http://jsfiddle.net/XbGSV/63/