javascript或JavaScript等同于PHP的strtok()? [英] javascript or jquery equivalent of PHP's strtok()?

查看:121
本文介绍了javascript或JavaScript等同于PHP的strtok()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在与PHP strtok()等效的JavaScript?在PHP中,我将使用

Is there a javascript equivalent to PHPs strtok()? In PHP, I'd use

$sub = strtok($string, ':'); 

(谢谢Phil的回答 )

但是,我需要在javascript中做同样的事情.

However, I need to do the same in javascript.

我有一个选择列表,其中包含这样的值:

I have a select list that contains values like this:

<select id="myFonts">
    <option value='Leckerli One'>Leckerli One</option>
    <option value='Lekton:regular,italic,bold'>Lekton (plus italic and bold)</option>
    <option value='OFL Sorts Mill Goudy TT:regular,italic'>OFL Sorts Mill Goudy TT (plus italic)</option>
</select>

在jQuery change事件中,我需要提取值"字段.但是,如果value字段包含冒号,则需要在冒号之前提取字符串内容,然后在其后不返回任何内容.

And in the jQuery change event, I need to extract the "value" field. However, if the value field contains a colon, I need to pull the string contents before the colon and return nothing after it.

例如,在上面的示例中,我需要返回值:

For instance, in my example above, I need the values returned:

莱克利一号 莱克顿 OFL对Milled Goudy TT进行排序

Leckerli One Lekton OFL Sorts Mill Goudy TT

这是我当前的jQuery(当值包含冒号和其他属性时无法正常工作):

Here's my jQuery currently (does not work properly when value contains the colon and additional properties):

$('#myFonts').change
(
    function()
    {
    var myFont = $('#myFonts :selected').val();
    $('#fontPreviewSrc').attr('href','http://fonts.googleapis.com/css?family='+myFont);
    $('.fontPreview').attr('style','font-family:'+myFont+';font-style:normal;font-size:2em;padding-top:10px;white-space:nowrap');
    }
);

推荐答案

使用:

$('#myFonts :selected').val().split(':')[0]

这篇关于javascript或JavaScript等同于PHP的strtok()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆