最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Set text to dd element in javascript - Stack Overflow

programmeradmin1浏览0评论

I'd like to set text to dd element. I've tried to google that question, but found nothing.

Could you please tell me, how can I set(replace) text inside dd tag?

I've tried:

$('#dd_di').innerHTML = my_var; 
$('#dd_di').innerText = my_var; 
$('#dd_di').innerText = "3500"; 

but nothing changed.

console.log($('#dd_di')); 

[<dd id="dd_di"></dd>]

I'd like to set text to dd element. I've tried to google that question, but found nothing.

Could you please tell me, how can I set(replace) text inside dd tag?

I've tried:

$('#dd_di').innerHTML = my_var; 
$('#dd_di').innerText = my_var; 
$('#dd_di').innerText = "3500"; 

but nothing changed.

console.log($('#dd_di')); 

[<dd id="dd_di"></dd>]
Share Improve this question asked Jun 24, 2014 at 10:42 Aleksandr K.Aleksandr K. 1,41516 silver badges24 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You're trying to use DOM properties on a jQuery object. In jQuery, use .text() or .html() to set the text and raw HTML of an element respectively.

$('#dd_di').text(my_var);
$('#dd_di').html(my_var); // or if you need to set the raw HTML

innerHTML and innerText can be used, but you need to access the DOM element:

$('#dd_di').get(0).innerHTML = my_var;

// or no jQuery:
document.getElementById("dd_di").innerHTML = my_var;

use $('#element_id').html(), do:

$('#dd_di').html("your text");

demo:: jsFiddle

try this:

 $(document).ready( function() {
$('#dd_di').text("custom text"); 
});

<dd id="dd_di">hello</dd>

http://jsfiddle/5RPaZ/

You can use the DOM properties on the DOM objects:

window.dd_di.innerHTML = my_var;
window.dd_di.innerText = my_var;

DOM elemnets with are available in the global (window) objects. You can also be more explicit:

document.getElementById('dd_di').innerHTML = my_var;
document.getElementById('dd_di').innerText = my_var;

Both work without jQuery. Normally you'd consistently use either jQuery (see other answers) or native DOM api.

发布评论

评论列表(0)

  1. 暂无评论