选择jquery元素选择器内的元素

问题描述 投票:1回答:3

我有一个div

   <div id="dialog-confirm-error-validating-choices" title="Pop up">
        <p><span class="floatLeft"></span>
        <p id="error-message"></p></p>
    </div>

我用jQuery选择并将其放入变量中

var messageDialog = $('#dialog-confirm-error-validating-choices');

我想在此变量中设置元素的内部html

messageDialog.filter('#error - message').innerhtml("Hello");

这是怎么做的?可以吗?

jquery html
3个回答
2
投票

试试这个:

var $messageDialog = $('#dialog-confirm-error-validating-choices');
$messageDialog.find('#error-message').html("Hello");
// alternative: $('#error-message', $messageDialog).html("Hello");

请注意,innerHtml是更改普通javascript元素的HTML的方法,而对于jQuery对象,您需要使用html()方法。此外,惯例是使用$为包含jQuery对象的变量添加前缀。


0
投票

试试这个。

$('#error-message').html("Hello")

请注意,函数html设置html

另请注意,我更新了您的查询,以匹配元素的ID


0
投票

考虑到您要选择的元素具有唯一ID,最有效的选择器将是其ID:

  $('#error-message').html('hello');
© www.soinside.com 2019 - 2024. All rights reserved.