jQuery更改嵌套的html元素

时间:2015-08-21 19:47:56

标签: jquery

我生成了一段错误的HTML代码:

public static void Log<T>(Expression<Func<T>> expression)
{
    string oldName = ((MemberExpression)expression.Body).Member.Name;
    object value = expression.Compile().Invoke();
}

如何在运行时使用jQuery更改此内容,如下所示:

set start [clock seconds];
run -all   
set finish [clock seconds];
puts [expr {$finish - $start}]

P.S。 h1元素中可以有任意数量的链接。

3 个答案:

答案 0 :(得分:0)

$('.title').after($('<p>').append($('.title').children())).html($('.title').text().trim());

编辑:如果你有多个&lt; h1&gt;元素,你可以遍历它们:

&#13;
&#13;
$(function() {
  $('.title').each(function() {
    $(this).after($('<p>').append($(this).children())).html($(this).text().trim());
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1 class="title">Some Value
    <a href="#">link1</a>
    <a href="#">link2</a>
    <a href="#">link3</a>
    <a href="#">link4</a>
</h1>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我不确定每个h1元素是否都有'title'类,所以我为每个h1实例实现了。

$("h1").each(function() {
    var innerhtml = "<p>";
    $(this).find("a").each(function() {
        innerhtml += $(this).html();
    });
    innerhtml += "</p>";
    $(this).html(innerhtml);
});

答案 2 :(得分:0)

到您的HTML

$(document).ready(function() {
    var links = $('h1.title a');
    $('h1.title').after('<p class="movehere"></p>');
    $('p.movehere').append(links);
    $('h1.title a').remove();
});

脚本:

#include <stdlib.h>
#include <stdio.h>

/* Linked list node */
struct node
{
    int data;
    struct node *next;
};

struct node *newNode( int data, struct node *next )
{
    struct node *tmp = ( struct node *) malloc( sizeof( struct node ) );

    if ( tmp )
    {        
        tmp->data = data;
        tmp->next = next;
    }

    return tmp;
}

/* Function to insert a node at the beginning of the Doubly Linked List */
void push( struct node **head_ref, int data )
{
    /* allocate node */
    struct node *tmp = newNode( data, *head_ref );

    if ( tmp != NULL )
    {        
        *head_ref = tmp;
    }
}

int find( struct node *head, int data )
{
    while ( head && head->data != data ) head = head->next;

    return head != NULL;
}    

struct node* list_union( struct node *first, struct node *second )
{
    struct node *head = NULL;
    struct node **head_ref = &head;

    for ( struct node *current = first; current != NULL; current = current->next )
    {
        struct node *tmp = newNode( current->data, NULL );
        if ( tmp != NULL )
        {
            *head_ref = tmp;
            head_ref = &( *head_ref )->next;
        }
    }

    for ( struct node *current = second; current != NULL; current = current->next )
    {
        if ( !find( first, current->data ) )
        {                       
            struct node *tmp = newNode( current->data, NULL );
            if ( tmp != NULL )
            {
                *head_ref = tmp;
                head_ref = &( *head_ref )->next;
            }
        }           
    }

    return head;
}    

void printList( struct node *node )
{
    for ( ; node != NULL; node = node->next )
    {
        printf( "%d ", node->data );
    }

    printf("\n");
}

/* Drier program to test above function */
int main( void )
{
    struct node *first  = NULL;
    struct node *second = NULL;
    struct node *union2 = NULL;

    // create first list 7->5->9->4->6
    push( &first, 6 );
    push( &first, 4 );
    push( &first, 9 );
    push( &first, 5 );
    push( &first, 7 );

    printf( "First list is: " );
    printList( first );

    push( &second, 6 );
    push( &second, 4 );
    push( &second, 9 );
    push( &second, 11 );
    push( &second, 12 );

    printf( "second list is: " );
    printList( second );

    union2 = list_union( first, second );

    printf( "Union of 2 lists is: " );
    printList( union2 );

    // Here you should call a function that frees all the memory allocated for the lists
}    

http://codepen.io/anon/pen/KpjbpM