简体   繁体   English

如何使用JavaScript更改标签

[英]How change tags with javascript

We have a project that demands an single page application. 我们有一个需要单页应用程序的项目。 For the moment I have this HTML / carousel page to create questions. 目前,我有这个HTML /轮播页面来创建问题。 As you can see, there is no input at all. 如您所见,根本没有输入。 this must be made possible through javascript. 必须通过javascript使它成为可能。 with an onclick event every H you see, has to be converted to an input tag when you click on the tag itself. 您看到的每个H都有一个onclick事件,当您单击标记本身时,必须将其转换为输入标记。

Any help? 有什么帮助吗? I thought I had figured a way out, but it doesn't work 我以为我已经找到了出路,但这没用

HTML: HTML:

  <div>
      <div ng-controller="CarouselDemoCtrl">
          <div style="height:10px;">
              <carousel interval="myInterval">
                  <slide ng-repeat="slide in slides" active="slide.active" >
                  <div class="CreateSlideCss">
                    <h1>
                        <div class="CreationTitel">
                            Click om uw titel op te geven
                        </div>
                    </h1>
                    <h2>
                        <div class="CreationVraag"> 
                            Click om uw vraag in te voeren .
                        </div>
                    </h2>
                    <button class="CreationVraagBtn" >click voor uw type vraag te selecteren</button>
                    <h3>
                        <textarea class="CreationAntwoordTxt" >hier is de content van uw antwoorden</textarea>
                    </h3>
                          <div class="carousel-caption">
                              <h4>Slide {{$index}}</h4>
                          </div>
                      </div>
                  </slide>
              </carousel>
          </div>
          <div class="row" >
              <div class="col-md-6">
                  <button type="button" class="btn btn-info" ng-click="addSlide()">Add Slide</button>
              </div>
          </div>
      </div>
  </div>

jQuery: jQuery的:

$( "button.CreationVraagBtn" ).click(function() {
    $( "div.CreationTitel" ).replaceWith( "<input value='" + "hallo" + "'></input>" );
});

You can update your click to this: 您可以将点击更新为:

$('h1, h2, h3, h4, h5, h6').on('click', function(){
    $(this).html('<input type="text" />');
});

and more angular way, you can use this in your directive : 以及更多角度的方式,您可以在directive

.directive('carousel', function() {
    return {
       restrict: 'E',
       templateUrl: function(elem, attr){
          angular.element(elem).find('h1, h2, h3, h4, h5, h6').on('click', function(){
              $(this).html('<input type="text" />');
          });
       }
    };
});

As per docs: 根据文档:

If jQuery is available, angular.element is an alias for the jQuery function. 如果有jQuery,则angular.element是jQuery函数的别名。 If jQuery is not available, angular.element delegates to Angular's built-in subset of jQuery, called "jQuery lite" or "jqLite.". 如果没有jQuery,则angular.element会委托Angular的内置jQuery子集,称为“ jQuery lite”或“ jqLit​​e”。


jqLite is a tiny, API-compatible subset of jQuery that allows Angular to manipulate the DOM in a cross-browser compatible way. jqLit​​e是jQuery的一个很小的,与API兼容的子集,它允许Angular以跨浏览器兼容的方式操作DOM。 jqLite implements only the most commonly needed functionality with the goal of having a very small footprint. jqLit​​e仅实现最常用的功能,目的是占用很小的空间。

jqLite is already implemented in angular which has the most commonly used methods. jqLit​​e已经以angular的方式实现,它具有最常用的方法。 if you load jQuery before angular then you will make use of fully functional jQuery methods. 如果您在angular之前加载jQuery,则将使用功能齐全的jQuery方法。

Simply add an id to the enclosing element, remove the current content, and insert your new node using the DOM API. 只需向封闭元素添加一个id,删除当前内容,然后使用DOM API插入新节点。

So for example, you give it the id myId , then you can do this: 因此,例如,您为其指定id myId ,则可以执行以下操作:

var node = document.querySelector('#myId');
while (node.firstChild) {
    node.removeChild(node.firstChild);
}
var newNode = document.createElement('input');
// set up attributes
newNode.setAttribute('type', 'text');
newNode.setAttribute('value', 'hallo');
node.appendChild(newNode);

Or in a jQuery way: 或以jQuery方式:

$('#myId').empty();
$('#myId').append($('<input type="text" value="hallo"/>'));

A very simple option would be to style an input with a placeholder to display like normal text would, as long as it has no focus: 一个非常简单的选项是使用占位符设置输入的样式,使其与普通文本一样显示,只要它没有焦点即可:

 input{ border:none; color:black; } input:focus{ border: 1px solid black; } 
 <input type="text" placeholder="Click om uw titel op te geven"/> 

You could also style the placeholder text , if you wish. 如果需要,还可以设置占位符文本的样式


And I see you're using AngularJS, that makes this a lot easier: 而且我看到您正在使用AngularJS,这使此操作变得容易得多:

A toggle: 切换:

<div class="CreationTitel">
    <span ng-click="showTitleInput = !showTitleInput">Click om uw titel op te geven</span>
    <input type="text" ng-show="showTitleInput"/>
</div>

Or just a "show" element / button that disappears: 或者只是消失的“显示”元素/按钮:

<div class="CreationTitel">
    <span ng-click="showTitleInput = true" ng-hide="showTitleInput">Click om uw titel op te geven</span>
    <input type="text" ng-show="showTitleInput"/>
</div>

 $('h1, h2, h3, h4, h5').click(function() { $(this).replaceWith('<input>'+ this.innerHTML + '</input>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h1>text 1</hi> <h2>text 2</h2> <h1>text 3</h1> </div> 

I think the H tag should intelligent enough to render an input . 我认为H标签应该足够智能以呈现输入。 You can place a class like "renderinput" and then add data attributes like 您可以放置​​“ renderinput”之类的类,然后添加诸如

<h1 class="renderinput" data-pickdata=".CreationTitel" data-placeusing="text"></h1>

and js code would look like : 和js代码如下所示:

      $(function () {
        $('.renderinput').on('click',function(){
          var $this = $(this);
        var input = $('<input></input>').attr({
        'type' : 'text'
          })
        .val($($this.data('pickdata'))[$this.data('placeusing')]());
        $this.replaceWith(input);
        });
});

this will make sure that you play only with html rather than touching js in case another H tag is needed. 这将确保您仅使用html播放,而不需要需要其他H标签的情况下触摸js。

Script code must be placed inside the CarouselTag in order to make it work. 脚本代码必须放在CarouselTag内才能起作用。

perhaps an expert can give a verry clear explanation for this. 也许专家可以对此给出非常明确的解释。 although this is a solution for some who would like the same approach 尽管对于某些想要相同方法的人来说这是一个解决方案

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

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