1

在这种形式中,我有一条用户消息<span>。它忽略 HTML 和 CSS 样式表中给出的样式信息。此外,如果我更新 using .html() ,则不会显示新文本。

$('#matting').append('<div id="register_user_box" class="inline_form" style="position: absolute; top: 20px; right: 10px; <br/><span id="user_msg" style="width: 90%; height: 15px; color: red; font-family: Arial; font-size: 11px;">Info msg</span><br/><form id="register_user_id" name="register_user_form"> <button id="cancel" onclick="cancel_register(); return false;" style="width: 63px; float: right; margin-right:8px;">Cancel</button> <button id="register" onclick="register_user(); return false;" style="width: 63px; float: right;">Save</button> </form></div>');

在样式中为 user_msg 指定的信息将被忽略。如果我用它更新文本$('#user_msg').html(message);也会被忽略。这有什么可以解释的?

4

2 回答 2

1

HTML 语法错误 - 您没有关闭您的<div>或其中的样式属性:

$('#matting').append('<div id="register_user_box" class="inline_form"'+
  ' style="position: absolute; top: 20px; right: 10px; ' + // missing ">! 
  '<br/><span id="user_msg" style="width: 90%; height: 15px;'+
  'color: red; font-family: Arial; font-size: 11px;">Info msg</span> '+
  '<br/><form id="register_user_id" name="register_user_form">'+
  '<button id="cancel" onclick="cancel_register(); return false;"'+
  ' style="width: 63px; float: right; margin-right:8px;">Cancel</button>'+
  ' <button id="register" onclick="register_user(); return false;"'+
  'style="width: 63px; float: right;">Save</button> </form></div>');

另请注意<span>'s 是内联元素(请参阅scrappedcola 的答案)。

于 2012-04-18T19:44:28.070 回答
1

<span>是内联元素。要给它高度和宽度,请使用 cssdisplay:inline-block;要添加文本消息,请尝试使用$('#user_msg').text(message);

于 2012-04-18T19:47:07.000 回答