开发者

How to substring one by one in one button in php?

开发者 https://www.devze.com 2023-03-06 14:53 出处:网络
How to substring one by one in one button in php ? Example: $myvar=\"abc\"; i need one button when click 1 time=display a, click 2time di开发者_StackOverflow社区splay b, and click 3time

How to substring one by one in one button in php ? Example: $myvar="abc"; i need one button when click 1 time=display a, click 2time di开发者_StackOverflow社区splay b, and click 3time display c. click how many time ?depend on string of $myvar.

Best regards


sure you should look into javascript for that, sir.

anyway, i'll give you some codes, assuming the button has an id as "Btn";

var str = 'abc';
var n = 0;
var btn = document.getElementById('Btn');

btn.onclick = function() {
    alert(str.substring(n, n + 1));
    n = (n + 1) % 3;
};


Oh, i made a mistake. the semicolon at the end of "n = (n + 1) % 3" is not correct!(i carelessly used a chinese semicolon instead...)

var str = 'abc';
var n = 0;
var btn = document.getElementById('Btn');

btn.onclick = function() {
    alert(str.substring(n, n + 1));
    n = (n + 1) % 3;
};

this piece might be OK.


i assume the element you use to display has an id "Txt", which can be a "span" or a "div" or any other elements you like.

<a href="#" id="Btn">Click Me!</a>
<span id="Txt"></span>

---------------------------------------------------------------------------------------
function displayOneByOne(str) {
    var n = 0;
    var btn = document.getElementById('Btn')    //get the clickable element
    var txt = document.getElementById('Txt')    //get the area to display the character

    btn.onclick = function() {
        txt.innerHTML = str.substring(n, n + 1);
------------------------------------------------
        if (n < str.length) n++;
        else return false;
------------------------------------------------
    }
}

displayOneByOne("YOUR_STRING_HERE");
---------------------------------------------------------------------------------------


<a href="#" id="Btn">Click Me!</a>
<span id="Txt"></span>

---------------------------------------------------------------------------------------
function displayOneByOne(str) {
    var n = 1;
    var btn = document.getElementById('Btn')    //get the clickable element
    var txt = document.getElementById('Txt')    //get the area to display the character

    btn.onclick = function() {
        if (n <= str.length)
        txt.innerHTML = str.substring(0, n++);
        return false;
    }
}

displayOneByOne("YOUR_OWN_STRING");
--------------------------------------------------------------------------------------

if you input "abc" then click, it will display like this:

"a" --> "ab" --> "abc"

0

精彩评论

暂无评论...
验证码 换一张
取 消