javascript无法显示警报

时间:2017-08-31 12:31:53

标签: javascript jquery dom socket.io polymer

我有一个html元素,我想在点击“i”元素时显示警告。当具有类“innerNotif”的div直接放在DOM上时它可以工作,但是当我用socket添加div时,它不起作用。我认为这是因为元素在加载时不在DOM中。

<span class="disNone notificationMenuCss" id="notifBar">
   <div class="innerNotif" id="idNatif">1<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>
</span>

<script>
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(){
        var numItems = $('.innerNotif').length +1;

    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId +'"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
    Polymer({
        alertNotif: function() {
            alert('Test');
        }
    });
</script>

3 个答案:

答案 0 :(得分:2)

新行字符会破坏您想要追加的字符串。

这应该有效:

$( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId + '"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>'); 

答案 1 :(得分:0)

试试我编辑过的代码。

<span class="disNone notificationMenuCss" id="notifBar">
   <div class="innerNotif" id="idNatif">1<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>
</span>

<script>
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(){
        var numItems = $('.innerNotif').length +1;

    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId + '"><i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
    Polymer({
        alertNotif: function() {
            alert('Test');
        }
    });
</script>

答案 2 :(得分:0)

我通过添加父元素的onclick函数并在其上添加addEventListener来找到解决方案

<paper-toolbar class="header" on-click="removeNotif">
    <div class="notification" on-click="notificationMenu">
        <span class="notificationSpan" id="notificationNumber"></span>
        <span class="disNone notificationMenuCss" id="notifBar"></span>
    </div>
</paper-toolbar>

<script type="text/javascript">
var socket = io.connect();
var notifId = 0;

socket.on('libCourtLotChangeServ', function(contrat){
    var numItems = $('.innerNotif').length +1;
    $( "#notifBar" ).append( '<div class="innerNotif" id="id' + notifId +'" style="width: 96%; height: 30px; text-align: left; border: 1px solid black; background-color: white; padding: 3px;">' + 'Le contrat <br>' + contrat + ' a été modifié ' + '<i class="fa fa-times" on-click="removeNotif" aria-hidden="true"></i></div>');
    notifId++;
    $("#notificationNumber").html(numItems);
});
</script>

<script>
 Polymer({
     removeNotif: function() {
         var idValue = Number(($(".innerNotif:last").attr("id")).replace("id", "0")); 
         console.log(idValue);
         for (var numItemsCheck = 0; numItemsCheck <= idValue; numItemsCheck++){ 
             document.querySelector("#id" + numItemsCheck).addEventListener("click", function(){
                 var numItems = $('.innerNotif').length -1;
                 $("#notificationNumber").html(numItems);
                 this.remove();
              })
          }
      }
  });
  </script>