如何获得跨度的价值和属性

时间:2016-02-05 06:50:49

标签: jquery

我的代码是:

<br>
var value = $("#editor").jqxEditor('val');

值=&#34; <div><span style="font-weight:bold">Welcome</span></div>&#34 ;;

那么我如何在变量中获得span文本及其属性font-weight值。

4 个答案:

答案 0 :(得分:0)

使用find方法:

var value = $("#editor").jqxEditor('val');
var span =  value.find('span');

要获取其属性,您可以使用attr方法:

var attrs = span.attr('style');

答案 1 :(得分:0)

字体重量400与正常相同,700与粗体

相同
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>
    $(document).ready(function () {

        value = '<div><span class="test_span" style="font-weight:bold">Welcome</span></div>';
        alert($(value).find('span.test_span').text());
        alert($(value).find('span.test_span').css('font-weight'));

    });
</script>
</head>
<body>

</body>
</html>

如果你没有课,那么请使用此代码

$(document).ready(function () {
        value = '<div><span style="font-weight:bold">Welcome</span></div>';
        alert($(value).find('span').text());
        alert($(value).find('span').css('font-weight'));
    });

答案 2 :(得分:0)

将类添加到span.let我说class =&#34; foo&#34 ;;

$('.foo').text(); //to get value
$('.foo').attr();//to get attribute

答案 3 :(得分:0)

尝试转换jquery对象中的文本,并根据该对象获取文本和喜欢的权重:

var value='<div><span style="font-weight:bold">Welcome</span></div>';
var text = $(value).find('span').text();
var fontWeight = $(value).find('span').css('font-weight');
console.log(text+' '+fontWeight)

见:https://jsfiddle.net/43xnfpqu/