jquery / javascript - 简单的split()问题

时间:2011-06-27 08:59:05

标签: javascript jquery regex

if ($(".productpage .description").html() != null) {

    var textToHide = $('.productpage .description').html().split('<br class="breakHere">')[1];
    var visibleText = $('.productpage .description').html().split('<br class="breakHere">')[0]; 
}

在Firefox中运行良好,但在IE和Chrome中,textToHide和visibleText未定义。我错过了什么?感谢

2 个答案:

答案 0 :(得分:1)

查看您的$('.productpage .description').html(),您可能会收到<br />

答案 1 :(得分:1)

对HTML使用split()不是一个好主意恕我直言 - 它太脆弱了,可能会遇到与使用Regexp对抗HTML相同的问题。

假设您尝试拆分的元素都是<br>元素的兄弟,请尝试:

var $el = $('.productionpage .description'); // find the element
var $br = $('br.breakHere', $el);            // find the BR inside it

var $vis = $br.prevAll();             // contains everything before the break
var $hide = $br.nextAll();            // contains everything after the break

请注意,这将为您提供包含这些元素的jQuery对象,而不是它们的HTML文本。