用javascript移动li元素

时间:2017-10-03 10:40:08

标签: javascript jquery html css

我想在具有相同类的li列表之后移动一个li元素,这可能吗?

$("li#anonymous_element_2").insertAfter("li.amscheckout-row:last");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="amscheckout-row"> </li>
<li class="amscheckout-row"> </li>
<li class="amscheckout-row"> </li>
<li class="fields" id="anonymous_element_2"> </li>
<li class="amscheckout-row"> </li>
<li class="amscheckout-row"> </li>
<li class="amscheckout-row"> </li>
<li class="amscheckout-row"> </li>

3 个答案:

答案 0 :(得分:2)

jQuery有一个内置的import React, { Component } from 'react'; import List from './src/List'; var items = require('./assets/shopping.json'); import { AppRegistry, StyleSheet, Text, View } from 'react-native'; export default class AwesomeProject extends Component { render() { return ( <View style={ styles.container } > <List style = { listStyles.container } items={items} align-items="center" /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', flexDirection: 'row', backgroundColor: '#000000', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, } }); const listStyles = StyleSheet.create({ container: { justifyContent: 'center', alignItems: 'center', flexDirection: 'row', maxHeight: '60%', display: 'flex', backgroundColor: '#FFFF00', } }); AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); 函数:

last()
var elementToMove = $('#anonymous_element_2');
var targetPosition = $('.amscheckout-row').last();

elementToMove.insertAfter(targetPosition);

答案 1 :(得分:1)

是的,你可以! 符合您要求的方法是:after。 如果我理解你想要&#34;移动&#34;并且,要做到这一点,你必须&#34;删除/添加&#34;将元素放入值列表中。 我建议你试试这段代码:

var $liToAppend = $('li#anonymous_element_2');
var $lastLi = $('ul li:last'); // I suppose you are using the root tag UL!

// The final solution:
$liToAppend.remove();
$lastLi.after($liToAppend);

使用jQuery,你可以通过很多方式解决这个问题:

$('li#anonymous_element_2').remove().appendTo('ul');

也应该工作!

答案 2 :(得分:0)

试试这个

<script>
 $("<li>Hello world!</li>").insertAfter(".amscheckout-row:last")
</script>