简体   繁体   English

变量值未附加在h2元素中

[英]variable value is not appending in h2 element

DEMO 演示

Hi, I have variable called "var node = "Hi Mr", Iam trying to append this value on click of a button, im  not sure why its not appending. 
I tried using html and text, none worked.

JS: JS:

$(function(){
    $('.customerResultIndex').on('click',function(){
        openCustomerOverlay();
    })

});
function openCustomerOverlay(){
    var node = "Hi Mr"
    $('.customerPopHeading').text(node)

    var overlayContainer = 
    '<div class="customerOverlayShadow">'+
        '<div class="customerOverlay borderRadius10px">'+
            '<h2 class="customerPopHeading">-- </h2>'+

Create the element first, them after this, set the text... You are setting the text befero creating it. 首先创建元素,然后创建元素,然后设置文本...您正在设置文本,然后再创建它。

Put this: 把这个:

var node = "Hi Mr";
$('.customerPopHeading').text(node);

After this: 在这之后:

$(document).keyup(function(e) {
    if (e.keyCode == 27) {  
        $('.customerOverlayShadow').remove();   
    }
}); 

Final result: http://jsfiddle.net/pc2tx1us/3/ 最终结果: http : //jsfiddle.net/pc2tx1us/3/

You cannot append a value like this instead you can concatenate it this way: 您不能附加这样的值,而是可以通过以下方式将其连接:

'<h2 class="customerPopHeading">--'+ node +'</h2>'+

or you can move this line: 或者您可以移动以下行:

$('.customerPopHeading').text(node);

below this: 在此以下:

$("body").prepend(overlayContainer).focus();
$('.customerPopHeading').text(node);

Fiddle 小提琴

The element has to be available to make a jQuery selector, here when you prepend in body only then it is available to put the desired text in it. 该元素必须可用于创建jQuery选择器,此处仅当您在正文中开头时,才可以在其中添加所需的文本。

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

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