English 中文(简体)
如何在用javascript修改酋长领地时只修改“文字”
原标题:How to only modify "text" while modifying the dom using javascript
  • 时间:2009-10-16 06:25:35
  •  标签:

我试图用 j子写出一个“不可打破”的班子,以便:

<span class=unbreakable>
   some text in here 
   maybe with a <span title="tag">tag</span> or something
</span>

每个空间将由<代码>&nbsp;替换。 问题在于,如果我这样做的话。

$(".unbreakable").html
(   replaceAll($(".unbreakable").html(), " ", "&nbsp;")
);

这还取代各标签的空位——没有好处。 因此,我寻求一种办法,在不写文章的情况下避免tag头。 任何人都知道这样做的好办法?

最佳回答

与安保部的良好指导。 现在如何解决?

.unbreakable { white-space: nowrap; }
问题回答

增加:

.unbreakable {
    white-space: pre;
}

......你们的风格更容易?

[edit] fixed in response to a comment. This is probably not IE-compatible; haven t tested on anything other than Firefox. C.f. How do I select text nodes with jQuery.

<script language="JavaScript" type="text/javascript" src="jquery.js" ></script>
<link rel="stylesheet" href="styledButton.css" type="text/css" >

<script type="text/javascript">
  $(document).ready(function () {
    var NBSP =  u00a0 ;
    $(".unbreakable").css("border", "solid red 1px").contents().each(function(x){
      if (this.nodeType == Node.TEXT_NODE) {
        this.nodeValue = this.nodeValue.replace(/ /g, NBSP);
      }
    });
  } );
</script>
</head>

<body>

<div id="log" style="width: 10px">
  <span class="unbreakable">testing <span title="Moo">XXX YYY</span> testing 1 2 3</span>
  <span class="breakable">(Now this is a breakable span)</span>
  <span class="unbreakable">testing again...</span>
</div>

<div id="A"></div>

</body>




相关问题
热门标签