简体   繁体   English

jQuery的更改HTML标签

[英]Jquery change html tag

I have structure like this: 我有这样的结构:

<div id="mali_oglasi">
    <?php foreach ($mgs as $key) : ?>
    <p class="mali_oglas">
        <span class="name"><?php echo $key['name'] ?></span>
        <span class="body"><?php echo $key['body'] ?></span>
        <span class="contact_author"><?php echo $key['contact_author'] ?></span>
        <span class="date_created"><?php echo $key['date_created'] ?></span>
        <span class="date_expires"><?php echo $key['date_expires'] ?></span>
        <span class="email"><?php echo $key['email'] ?></span>
    <?php foreach ($lokacija as $lok) : ?>
    <?php if($lok['id_location'] == $key['location_id']) : ?>
    <span><?php echo $lok['name'] ?></span>
    <?php endif ?>
    <?php endforeach; ?>

    <?php foreach ($kategorija as $kat) : ?>
    <?php if($kat['id_category'] == $key['category_id']) : ?>
    <span><?php echo $kat['name'] ?></span>
    <?php endif ?>
    <?php endforeach; ?>
    <a class="obrisi" id="<?php echo $key['id_global_info'] ?>">Obrisi</a>     
    <a class="izmeni" id="<?php echo $key['id_global_info'] ?>">Izmeni</a>  
</p>
    <?php endforeach; ?>
</div>

When <a class="izmeni" id="<?php echo $key['id_global_info'] ?>">Izmeni</a> is clicked it need to change span to input or textarea. 单击<a class="izmeni" id="<?php echo $key['id_global_info'] ?>">Izmeni</a> ,需要将跨度更改为输入或文本区域。 How can I do this? 我怎样才能做到这一点?

You would use jQuerys .replaceWith() -method. 您将使用jQuerys .replaceWith() -方法。 Eg 例如

$('.izmeni').on('click', function() {
    $('span').replaceWith('<input />');
});

You can also walk up the DOM in your click handler to get to your target element: 您还可以在点击处理程序中浏览DOM以获得目标元素:

$('.izmeni').on('click', function() {
    $(this).closest('span').replaceWith('<input />');

    // or:
    //  $(this).closest('p').find('span').replaceWith('<input />');
    // etc.
});

Or something similar. 或类似的东西。 Take a look at jQuerys various tree traversal functions . 看一下jQuerys的各种树遍历函数

Use the click handler : 使用点击处理程序:

$(document).ready(function() {
    $('#<?php echo $key['id_global_info'] ?>').click(function() {
       $(selectorforspan).html('the new html');
    });
});

This could use the .html() function to set the HTML contents of the element specified in the selector ( $(selectorforspan) ). 这可以使用.html()函数设置在选择器( $(selectorforspan) )中指定的元素的HTML内容。

You should do 你应该做

$('a.izmeni').click(function(){
    $('span.classofyourspan').replaceWith($('<input>', { type : "text", name: "yourname"}));
});

There are a few ways you could do, but using jQuery's replaceWith method looks like the best way. 您可以采用几种方法,但是使用jQuery的replaceWith方法看起来是最好的方法。 There's even an example on their documentation: http://api.jquery.com/replaceWith/ 他们的文档中甚至还有一个示例: http : //api.jquery.com/replaceWith/

$('.izmeni').click(function() {
    $(this).replaceWith('<span>'+$(this).text()+'</span>');
});
$('a.izmeni').live('click', function() {
 var span = $('p.mali_oglasi span');
 var inputBox = $('<input />').attr('value', span.text());

 span.replaceWith(inputBox);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM