简体   繁体   English

Javascript DOM控制台浏览器

[英]Javascript DOM Console browser

I tried many solution but it seems not working for me could anyone help me how to resolve my problem? 我尝试了许多解决方案,但似乎对我不起作用,有人可以帮助我解决问题吗?

I want to change the GPS Fleet Management System value into textarea . 我想将GPS Fleet Management System的值更改为textarea

Here is my code: 这是我的代码:

<div class="caption">
  <i class="fa fa-reorder"></i>GPS Fleet Managment System
</div>
<a href="#" id="change">Change</a>

var textbox = $("#caption");
var textarea = $("<textarea id='textarea'></textarea>");
$("#change").click(function () {
   textbox = textbox.value(textarea);
});

Change value to html , add e.preventDefault() and change the #caption selector to .caption : 更改valuehtml中,添加e.preventDefault()并更改#caption选择到.caption

var textbox = $(".caption");
var textarea = $("<textarea id='textarea'></textarea>");
$("#change").click(function (e) {
    e.preventDefault();
    textbox.html(textarea);
});

A different approach could be: 一种不同的方法可能是:

 $(function () { var textbox = $("#caption"); var textarea = $("<textarea id='textarea'></textarea>"); $("#change").click(function (e) { var ele = document.getElementsByClassName('caption')[0].childNodes[2]; document.getElementsByClassName('caption')[0].replaceChild(textarea[0], ele); }); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <div class="caption"> <i class="fa fa-reorder"></i>GPS Fleet Managment System </div> <a href="#" id="change">Change</a> 

use e.preventDefault() to prevent the default click event,to get the textarea value use textarea.val(); 使用e.preventDefault()防止默认的click事件,要获取textarea值,请使用textarea.val(); e.preventDefault()

var textbox = $(".caption");
var textarea = $("<textarea id='textarea'></textarea>");
$("#change").click(function (e) {
        e.preventDefault();
        console.log(textarea.val());
        //to add the textare to the page use append() or html()
       textbox.append(textarea); 
});

If you want to replace the div with a textarea, use http://api.jquery.com/replaceWith/ . 如果要用文本区域替换div,请使用http://api.jquery.com/replaceWith/ Your var textbox = $("#caption"); 您的var textbox = $("#caption"); should be .caption since your element contains a class of caption, not an ID. 应该是.caption因为您的元素包含标题类别,而不是ID。

textbox.replaceWith(textarea);

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

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