Button positioning does not work

I created a button, so when I click on it, a random map appears. On a random card (s) there is an "x". I cannot put an "x" in the upper right corner, and I do not know why it does not work.

I want to create a function so that clicking on the "x" random card is deleted.

Here is my HTML:

<button class="plus">
  <div class="item">
    <p> + </p>
  </div>
</button>

<div id="newCardHolder">

</div>

    <div id="cardPrototype" class="card" style="display:none;">
        <p  class="delete">x</p>
      <span> Title </span>
      <form name="theform" style="display:none;">
        <input class="input-feld" type="text">
        <br>
        <input class="input-feld " type="text">
        <br>
        <input class="speichern"type="button" onClick="new Person()" value="Speichern">
        <input class="abbrechen"type="button" onClick="new Person()" value="Abbrechen">
      </form>
    </div>

My CSS:

.input-feld {
    font-family: TheSans Swisscom;
    margin:3px;
}

.card {
            width:300px;
            margin-right:5px; 
            margin-left:5px; 
            margin-bottom:10px; 
            float: left;
            padding:10px; 
            background-color: #BBBBBB; 
            border: 1px solid #ccc; 
            border-radius:10px;
}

.delete {
            font-family:'TheSans Swisscom';
            right:0;
            top:0;
}

.speichern{
    font-family:'TheSans Swisscom';
}

.abbrechen{
    font-family:"TheSans Swisscom";
    background-color: greenyellow; 
}

And my jQuery:

$(document).ready(function () {
    $("button.plus").on("click", function () {
        var newCard = $('#cardPrototype').clone(true); 
        $(newCard).css('display', 'block').removeAttr('id');
        $('#newCardHolder').append(newCard);
    });

    $('body').on('click', '.card', function () { 
        $(this).find('form').show();
        $(this).find('span').remove();
    });
});
+4
source share
4 answers

To place your little xCSS with this, you need to set it relative to its parent, which is a little contrary to intuition, if you haven’t done so already.

.card {
  position: relative;
}

.card .delete {
  position: absolute;
}

, jQuery , :

$('body').on('click', '.card .delete', function () { 
    $(this).closest('.card').remove();
});

, .

+5

position, . :

.card { 
       position: relative;
}
.delete {
       position: absolute;
       font-family:'TheSans Swisscom';
       right:0;
       top:0;
}

.card relative, .delete, , absolute.

+3

position: absolute .delete. , position: relative . .

+2

, CSS, .

$(document).ready(function() {
  $("button.plus").on("click", function() {
    var newCard = $('#cardPrototype').clone(true);
    $(newCard).css('display', 'block').removeAttr('id');
    $('#newCardHolder').append(newCard);
  });

  $('body').on('click', '.card', function() {
    $(this).find('form').show();
    $(this).find('span').remove();
  });
});
.input-feld {
  font-family: TheSans Swisscom;
  margin: 3px;
}

.card {
  position: relative;
  width: 300px;
  margin-right: 5px;
  margin-left: 5px;
  margin-bottom: 10px;
  float: left;
  padding: 10px;
  background-color: #BBBBBB;
  border: 1px solid #ccc;
  border-radius: 10px;
}

.delete {
  font-family: 'TheSans Swisscom';
  right: 5px;
  top: 0;
  position: absolute;
  display: block;
  margin: 0;
  line-height: 1;
}

.speichern {
  font-family: 'TheSans Swisscom';
}

.abbrechen {
  font-family: "TheSans Swisscom";
  background-color: greenyellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="plus">
  <div class="item">
    <p> + </p>
  </div>
</button>

<div id="newCardHolder">

</div>

<div id="cardPrototype" class="card" style="display:none;">
  <p class="delete">x</p>
  <span> Title </span>
  <form name="theform" style="display:none;">
    <input class="input-feld" type="text">
    <br>
    <input class="input-feld " type="text">
    <br>
    <input class="speichern" type="button" onClick="new Person()" value="Speichern">
    <input class="abbrechen" type="button" onClick="new Person()" value="Abbrechen">
  </form>
</div>
Hide result

position: relative; div card delete <p>. "button" <span> <i> . margin: 0; .., . , $(this).find('span').remove(); , . , <i>, , , font-style: normal;.

+1

Source: https://habr.com/ru/post/1652957/


All Articles